我的代码如下所示:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
当我用一个简单的reloadData
替换这个reloadRowsAtIndexPaths:withRowAnimation:
消息时,它工作得很好。
有什么想法吗?
发布于 2011-10-18 18:03:25
我认为下面的代码可能会起作用:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
发布于 2016-02-07 13:59:34
我遇到了这个问题,它是由一个调用reloadRowsAtIndexPath:withRowAnimation:的块和一个调用reloadData的并行线程引起的。崩溃是由于重载reloadRowsAtIndexPaths:withRowAnimation发现了一个空表,尽管我已经正常检查了numberOfRowsInSection & numberOfSections。
我采取的态度是,我并不真的关心它是否会导致异常。作为应用程序的用户,我可以忍受视觉上的损坏,而不是整个应用程序崩溃。
这是我对此的解决方案,我很乐意分享,并欢迎建设性的批评。如果有更好的解决方案,我很想听听?
- (void) safeCellUpdate: (NSUInteger) section withRow : (NSUInteger) row {
// It's important to invoke reloadRowsAtIndexPaths implementation on main thread, as it wont work on non-UI thread
dispatch_async(dispatch_get_main_queue(), ^{
NSUInteger lastSection = [self.tableView numberOfSections];
if (lastSection == 0) {
return;
}
lastSection -= 1;
if (section > lastSection) {
return;
}
NSUInteger lastRowNumber = [self.tableView numberOfRowsInSection:section];
if (lastRowNumber == 0) {
return;
}
lastRowNumber -= 1;
if (row > lastRowNumber) {
return;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
@try {
if ([[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] == NSNotFound) {
// Cells not visible can be ignored
return;
}
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@catch ( NSException *e ) {
// Don't really care if it doesn't work.
// It's just to refresh the view and if an exception occurs it's most likely that that is what's happening in parallel.
// Nothing needs done
return;
}
});
}
发布于 2016-10-08 14:23:46
经过多次尝试,我发现"reloadRowsAtIndexPaths“只能在某些地方使用,如果只改变单元格的内容,而不是插入或删除单元格。不是任何地方都可以使用它,即使你把它包起来
[self beginUpdates];
//reloadRowsAtIndexPaths
[self endUpdates];
我发现可以使用它的地方是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (IBAction) unwindToMealList: (UIStoryboardSegue *) sender
任何来自其他地方的尝试,比如从"viewDidLoad“或"viewDidAppear”调用它,要么不会生效(对于已经加载的单元格,我的意思是,重新加载不会生效),要么会导致异常。
所以试着只在那些地方使用"reloadRowsAtIndexPaths“。
https://stackoverflow.com/questions/4461490
复制相似问题