[英]Track the currently selected indexpath and the previously selected indexpath
我想在点击一次时在当前选定的行下方插入一个单元格,并在再次点击时删除该单元格。
如果用户点击新行,我还想从上一个选定行中删除插入的单元格。
为了实现这一点,我认为我需要跟踪当前选择的 indexpath 和之前选择的 indexpath,尽管我不知道如何实际实现这一点。
这是我到目前为止所拥有的:
- (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];
}
在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];
}
在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
在这一点上它有点工作; 当我 select 一个单元格时,我在其位置插入一个新单元格,当我再次点击它时,它会恢复为常规单元格。
但是,当我在选择前一个单元格一次后开始选择其他行时,我遇到了问题。
我不知道我这样做是否正确,我正在学习 go。
我想我会请这里的人帮助我。
你们能否帮助我并提供一个例子,说明如果没有问题我可以如何做到这一点,所以我可以理解如何做到这一点。
谢谢!
跟踪最后选择的路径
NSIndexPath *lastIndex = [table indexPathForSelectedRow];
并将最后一个索引与当前索引进行比较
if(([indexPath compare:lastIndex] == NSOrderedSame)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.