您好我正在使用聊天应用程序,每件事情都运行正常。现在我需要删除一条消息。因为我在tableview上使用长手势。但我的Tableview配置为部分是日期和消息是Rows.Now当Long Tap on row它显示的行号不符合Sections.So我在Code下面使用它显示当前的索引路径。然后我在sqlite中删除该消息。但是得到错误就像索引超越异常
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
id<SOMessage> message = self.conversation[indexPath.section][indexPath.row];
int index = (int)[[self messages] indexOfObject:message];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press on table view at row %d", index);
}
在上面的代码中,index是我删除行的indexapth值。我如何删除tableview中的行
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
下面是我的错误日志
invalid number of rows in section 1. The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
答案 0 :(得分:0)
也许这篇文章可能对您有所帮助 https://www.raywenderlich.com/77974/making-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1
答案 1 :(得分:0)
没有让你提问......你正在做的每一件事情,如果你能用这个删除sqlite中的行,那么你甚至可以在UITableView中。 要直接从表视图中删除行,只需在代码中添加以下内容:
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
}
我真的没有得到你的问题,但希望这可能有所帮助:)