BroSimple BroSimple - 1 month ago 15
Objective-C Question

Error when moving objects between NSMutableArray

In my iOS app, I have a table view and it has 2 sections/ 2 arrays. I am trying to move an object from one array/section to another array/section. Now Section 0 is called followedArray and Section 1 is called dataArray, dataArray stores all my data that make up each cell. So when the user clicks the button that I have set up called Follow, it's supposed to take that cell and remove it from dataArray/ Section 1 and insert it in followedArray/ Section 0. But when I try to do this I get an error that saids this.

Error Message


*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3599.6/UITableView.m:1396

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'


Code:

TableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configuring the cell
Data * dataObject;
if (!isFiltered) {

if (indexPath.section == 0) {
dataObject = [followedArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
dataObject = [dataArray objectAtIndex:indexPath.row];
}
}
else {
dataObject = [filteredArray objectAtIndex:indexPath.row];
}

// Loading Button
cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;

return cell;
}


Section Headers

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Followed Data";
}
else {
return @"All Data";
}
}


Number of Rows

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (!isFiltered) {

if (section == 0) {
return [followedArray count];
}
else if (section == 1) {
return [dataArray count];
}
}
return [filteredArray count];
}


-----------------------------------------------------

-- This is where the action happens --

-----------------------------------------------------

Follow Button

-(void)followButtonClick:(UIButton *)sender {

// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

// Creating an action per tag
if (indexPath != nil)
{
NSLog(@"Current Row = %@", indexPath);


// ----- ERROR HERE -----
[self.myTableView beginUpdates];

// ----- Inserting Cell to Section 0 ----- *NOT WORKING*
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
NSInteger rowToAdd = indexPath.row;
[self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

// ----- Removing Cell from Section 1 ----- *WORKING*
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

[self.myTableView endUpdates];

}
}

Answer

This is what helped me fix the errors, Credits to @AliOmari , @Rachel & @CodeChanger

    [self.myTableView beginUpdates];

    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

    [dataArray removeObjectAtIndex:indexPath.row];
    NSInteger rowToRemove = indexPath.row;
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    [self.myTableView endUpdates];