gpt4 book ai didi

ios - deleteRowsAtIndexPaths 后忽略 UITableView 的第一次点击

转载 作者:可可西里 更新时间:2023-11-01 03:32:23 24 4
gpt4 key购买 nike

我的 UITableView 有一组单元格。滑动单元格会导致出现删除按钮。点击这个 Remove 按钮会导致执行以下代码:

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.editing = NO;
  • 删除后调用[self.tableView reloadData];
  • 删除后调用[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
  • 在删除前调用[self.tableView beginUpdates];,在删除后调用[self.tableView endUpdates];
  • 同时完成以上所有工作

以上都没有帮助;删除后的第一次点击仍然始终被忽略。

更新:我还在上面的代码片段中添加了 [self.itemList removeObjectAtIndex:indexPath.row]; 代码。我的数据源委托(delegate)方法如下所示:

-(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 委托(delegate)中:

-(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];
}
}

这确实有效 - 我现在可以删除行,下一次点击也能正常工作。但是,这会删除我们对表格单元格的自定义外观,这首先是我们自定义代码的目的。也许我应该考虑使用上述方法并改为自定义“删除”按钮(我已经做了一些研究,但还没有找到好的答案)。

最佳答案

问题是,当您使用滑动删除时,只有您滑动的行会进入编辑模式。

第一次点击是结束该行的编辑模式:

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}

可以通过设置编辑模式来解决

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.itemList removeObjectAtIndex:indexPath.row];
[self.tableView setEditing:NO animated:YES];
}
[self.tableView reloadData];
}

关于ios - deleteRowsAtIndexPaths 后忽略 UITableView 的第一次点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20497349/

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