开发者

Track the currently selected indexpath and the previously selected indexpath

I want to insert a cell below the currently selected row when tapped once, and delete the cell when tapped again.

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

To achieve this i think i need to track the currently selected indexpath and the previously selected indexpath, though i don't know how to actually achieve this.

This is what i have so far:

- (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 point it kinda works; when i select a cell, i insert a new cell in its place, and when i tap it again, it reverts back to the regular cell.

I however get into problems when i start selecting other rows after just selecting the previous cell once.

I don't know if I'm doing this correctly, I'm le开发者_StackOverflow中文版arning as i go.

I thought i would ask the guys here to help me out.

Could you guys help me and provide an example of how i could do this if its no trouble, so i can understand how to do it.

Thanks!


to track last selected path

NSIndexPath *lastIndex = [table indexPathForSelectedRow]; 

and to compare last index with current index

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜