gpt4 book ai didi

iphone - 跟踪当前选择的indexpath和之前选择的indexpath

转载 作者:行者123 更新时间:2023-12-03 20:16:03 24 4
gpt4 key购买 nike

我想在点击一次时在当前所选行下方插入一个单元格,并在再次点击时删除该单元格。

如果用户点击新行,我还想从上一个选定行中删除插入的单元格。

为了实现这一点,我认为我需要跟踪当前选择的索引路径和先前选择的索引路径,尽管我不知道如何实际实现这一点。

这是我到目前为止所拥有的:

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

此时它有点起作用了;当我选择一个单元格时,我会在其位置插入一个新单元格,当我再次点击它时,它会恢复为常规单元格。

但是,当我在选择前一个单元格一次后开始选择其他行时,我会遇到问题。

我不知道我这样做是否正确,我正在边做边学。

我想我应该请这里的人来帮助我。

你们能否帮助我并提供一个示例,说明如果没有问题,我可以如何做到这一点,这样我就可以理解如何做到这一点。

谢谢!

最佳答案

跟踪最后选择的路径

NSIndexPath *lastIndex = [table indexPathForSelectedRow]; 

并将最后一个索引与当前索引进行比较

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

关于iphone - 跟踪当前选择的indexpath和之前选择的indexpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com