我的UITableView有一个单元集合。滑动单元格会导致出现“删除”按钮。点击此删除按钮将导致执行以下代码:第一次点击UITableView后忽略deleteRowsAtIndexPaths
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.itemList removeObjectAtIndex:indexPath.row];
这正确地导致从列表中删除单元格。但是,当我点击UITableVIew中剩余的单元格中的一个单元格来选择它时,该分支将被忽略(即不调用tableView:didSelectRowAtIndexPath)。如果我再次点击,则它可以正常工作(即调用tableView:didSelectRowAtIndexPath)。
删除后我点击哪个单元并不重要,无论我在删除之后等待多长时间,删除之后的第一次敲击总是被忽略,删除之后的第二次敲击成功。
基于各种StackOverflow的答案,我曾尝试:
- 删除
- 删除
- 删除
- 调用
[self.tableView beginUpdates];
之前删除后调用[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
后调用[self.tableView reloadData];
后设置self.tableView.editing = NO;
[self.tableView endUpdates];
删除后 - 在同一时间完成以上所有操作e
以上都没有帮助;删除后的第一次点击仍然始终被忽略。
更新: 我还在上面的代码片段中添加了[self.itemList removeObjectAtIndex:indexPath.row];
代码。 我的数据源委托方法是这样的:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.itemList.count;
}
中的代码片段被调用的doDelete方法来响应一个按钮龙头([self.deleteButton addTarget:self action:@selector(doDelete) forControlEvents:UIControlEventTouchUpInside];
)
更新#2:基于Lyssa的意见,我曾尝试以下:我删除我们的自定义按钮,删除所有引用和我们的滑动手势,然后将此代码添加到我们的UITableView委托:
-(BOOL) tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(void) tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.itemList removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
但这工作 - 我现在可以删除行和下一个水龙头确实工作正常但是,这消除了我们对表格单元格的自定义外观,这是我们自定义代码的目的。也许我应该看看使用上述方法并自定义删除按钮(我做了一些寻找,但没有找到一个好的答案呢)。
您是否从numberOfRowsInSection减少计数:删除单元格后。 – Greg
你是否更新了你的tableview的数据源,删除你删除的单元格的数据对象? – thorb65
你在哪里放了deleteRowsAtIndexPath? – asjj