There is a tableview consisting of two sections. Each of sections is filled from one of two arrays.

The task is to press the delete cell (for example, number 2) in the second section, the cell will be removed in the first - also under number 2, and in the reverse order, so that it also works

#import "ViewController.h" CGFloat tableViewHeaderHeigth = 50; CGFloat tableViewFooterHeight = 50; @interface ViewController () <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *firstArray; @property (nonatomic, strong) NSMutableArray *secondArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.firstArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", @"4", @"5", nil]; self.secondArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", @"4", @"5", nil]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView]; self.tableView = tableView; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //ΠΊΠΎΠ»-Π²Π° сСкций { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section==0) { return self.firstArray.count; } else if (section==1) { return self.secondArray.count; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *reUseId = @"Cell"; //Поиск ячСйки UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUseId]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUseId]; } if (indexPath.section == 0) { cell.textLabel.text = [self.firstArray objectAtIndex:indexPath.row]; } else { cell.textLabel.text = [self.secondArray objectAtIndex:indexPath.row]; } UIButton *button = [[UIButton alloc] init]; button.frame = cell.frame; button.backgroundColor = [UIColor clearColor]; [button addTarget:self action:@selector(didPressDeleteButton:) forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:button]; return cell; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } -(void)didPressDeleteButton:(id)sender{ [self.tableView setEditing:YES animated:YES]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { if (indexPath.section == 0){ NSMutableArray *tempArray = self.firstArray.mutableCopy; [tempArray removeObjectAtIndex:indexPath.row]; self.firstArray = tempArray.mutableCopy; } else { NSMutableArray *tempArray = self.secondArray.mutableCopy; [tempArray removeObjectAtIndex:indexPath.row]; self.secondArray = tempArray.mutableCopy; } [self.tableView reloadData]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

Update

A good example. If you press the sting of the fifth cell, then the 5th cell from the bottom section should be removed.

img

  • Did you write the code yourself or do not understand what is happening there? Remove if (indexPath.section == 0) { } else { and you will be happy - zhenyab
  • I wrote myself, I don’t understand how to delete the same index from the array as in the other - york4uk
  • [tempArray removeObjectAtIndex:indexPath.row] - and what does this line do? - zhenyab
  • this line is clear what it does - removes from the array, - york4uk

1 answer 1

I am a question stumped. Everything is already written in the code and it is not clear what caused the difficulties. Two identical arrays, the same index.

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // это индСкс, элСмСнт ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ ΠΈΠ· ΠΎΠ±ΠΎΠΈΡ… массивов int index = indexPath.row; // создаСм ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΠΌΡƒΡŽ копию ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ массива NSMutableArray *tempArray1 = self.firstArray.mutableCopy; // удаляСм элСмСнт находящийся ΠΏΠΎ индСксу [tempArray1 removeObjectAtIndex: index]; // замСняСм ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ массив Π½Π° Π½ΠΎΠ²Ρ‹ΠΉ self.firstArray = tempArray1.mutableCopy; // создаСм ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΠΌΡƒΡŽ копию Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ массива NSMutableArray *tempArray2 = self.secondArray.mutableCopy; // удаляСм элСмСнт находящийся ΠΏΠΎ индСксу. ИндСкс Ρ‚ΠΎΡ‚ ΠΆΠ΅ Ρ‡Ρ‚ΠΎ ΠΈ Π² ΠΏΠ΅Ρ€Π²ΠΎΠΌ массивС [tempArray2 removeObjectAtIndex: index]; // замСняСм Π²Ρ‚ΠΎΡ€ΠΎΠΉ массив Π½Π° Π½ΠΎΠ²Ρ‹ΠΉ self.secondArray = tempArray2.mutableCopy; [self.tableView reloadData]; } } 
  • Thank! It all worked! - york4uk