BroSimple BroSimple - 1 month ago 9
iOS Question

How to move a cell between 2 sections with a Button?

In my iOS app, I'm trying to move a cell from one section which uses an array (dataArray) to another section which uses another array (followedArray) with a button within each cell. I have the button set up but can't find any code to move cells with a button action.

I tried adding some code to add and remove objects from a NSMutableArray but I got an error





*** 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 goes in -(void)followButtonClick:(id)sender

Here is the code I tried adding to my button that goes in each cell by a tag since I get my arrays from a server and not manually typed in.

[myTableView beginUpdates];

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

// Inserting Cell to followedArray Section
[followedArray insertObject:indexPath atIndex:indexPath.row];
NSInteger rowToAdd = indexPath.row;
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

[myTableView endUpdates];





I'm sure this is wrong on many levels, but I'm drawing a blank here. Hopefully someone can point me to the right direction.

Code:

cellForRowAtIndexPath

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

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

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


Follow Button Click within each cell

-(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);

// Showing Status Labels
CustomCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
cell.firstStatusLabel.hidden = NO;
cell.secondStatusLabel.hidden = NO;

// Change Follow to Following
[sender setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
cell.followButton.hidden = YES;
cell.followedButton.hidden = NO;

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

// ----- Inserting Cell to Section 0 ----- *NOT WORKING*
[followedArray insertObject:[gamesArray 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*
[gamesArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

[self.myTableView endUpdates];
}
}


filteredArray is for my search bar, items are filtered from dataArray.

dataArray is where all my data is at from my server

followedArray is where I want to put the cell when the button is clicked from dataArray or filteredArray.

If you need more code, let me know, hopefully I can get this resolved, thank you!

Answer

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

    [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];
Comments