我花了几个小时寻找解决方案,但运气不错。我试图以编程方式删除一行(也取消选择相同的行)。在下面的行删除调用之后,期望调用UITableViewDelgate方法并更新数据源,但不刷新UITableView。 deselectRowAtIndexPath调用也不起作用。我尝试了各种场景,如注释行所示。
这是我的代码: checkoutPerson是由观察者监听NSNotificationCenter消息而调用的。
- (void) checkoutPerson: (NSNumber*) personId {
Person *person = [_people objectForKey:personId];
if( person )
{
// Remove person from data source
int rowIndex = person.rowIndex;
S2Log(@"Deleting row number=%d", rowIndex);
[_allKeys removeObjectAtIndex:rowIndex];
[_people removeObjectForKey: personId];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
//[[self tableView] beginUpdates];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
S2Log(@"Deleting indexPath row=%d", [indexPath row]);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//[[self tableView] endUpdates];
S2Log(@"Reloading data");
//[[self tableView] reloadData];
//[self performSelector:@selector(refreshView) withObject:nil afterDelay:1.5];
//[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}
}
我将感谢您的帮助。 谢谢 -Virendra
我相信删除的细胞不会被回收。如果我删除中间的行,则总是删除最后一行(因为只有一个项目),但删除的行仍然存在。
答案 0 :(得分:0)
在表视图的两个函数之间使用上面的代码
[tableView beginUpdates];
// the deletion code from data source and UITableView
[tableView endUpdates];
通过调用此函数,您告诉UITableView
您即将更新以删除您的单元格。
修改强>
我在您的代码中看到的另一个问题是您首先从数据源中删除数据。
现在您要求UITableViewCell
(实际上重新加载UITableView
)
然后你要删除UITableView
我猜您应该在删除数据源中的值之前获取UITableViewCell
。
答案 1 :(得分:0)
我发现了问题。它与我上面发布的代码无关。它是视觉显示和数据源内容之间的同步问题。我有一个嵌入式UITableView作为复合视图的一部分。在复合视图的控制器中,我将UITableView的委托和数据源连接到UITableViewController的实例。而不是这个,我应该将UITableViewController的tableView属性设置为嵌入式UITableView。似乎UITableView必须包含在UITableViewController中,以便正确地将表视图可视化显示同步到数据源的内容。这也修复了行取消选择和滚动。我还需要延迟reloadData调用,在这种情况下不需要deleteRowsAtIndexPaths:withRowAnimation。您所需要的只是修改数据源的内容并以1.5秒的延迟调用reloadData。
感谢所有人的大力帮助。