Tracking the currently selected index path and the previously selected index path

I want to insert a cell below the currently selected row with a single click and delete the cell when pressed again.

I also want to remove the inserted cell from the previous selected row if the user deletes a new row.

To achieve this, it seems to me that I need to track the currently selected index path and the previously selected index path, although I do not know how to achieve this.

This is what I still have:

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {

    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [self.theSelectedIndexes objectForKey:indexPath];

    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];

}

In didSelectRowAtIndexPath:

// I have a method that tracks if a cell is selected and if its deselected. This is what i'm using to insert a cell when selected and remove it when deselected.    


if ([self cellIsSelected:indexPath]) {

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
}else {
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
}

in cellForRowAtIndexPath:

// I use the same method to insert a new cell if the cell is selected or deselected

if [self cellIsSelected:indexPath]{
    // allocate new cell below selected cell
else
    // allocate standard view cell

At this moment, it works; when I select a cell, I insert a new cell in its place, and when I click it again, it goes back to the normal cell.

, , , , .

, , , .

, .

, , , , , , .

!

+3
1

NSIndexPath *lastIndex = [table indexPathForSelectedRow]; 

if(([indexPath compare:lastIndex] == NSOrderedSame)
0

All Articles