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 {
NSNumber *selectedIndex = [self.theSelectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
In didSelectRowAtIndexPath:
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.
, , , , .
, , , .
, .
, , , , , , .
!