首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >reloadRowsAtIndexPaths:withRowAnimation:使我的应用崩溃

reloadRowsAtIndexPaths:withRowAnimation:使我的应用崩溃
EN

Stack Overflow用户
提问于 2010-12-16 21:57:39
回答 7查看 23.5K关注 0票数 22

我的代码如下所示:

代码语言:javascript
复制
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

当我用一个简单的reloadData替换这个reloadRowsAtIndexPaths:withRowAnimation:消息时,它工作得很好。

有什么想法吗?

EN

回答 7

Stack Overflow用户

发布于 2011-10-18 18:03:25

我认为下面的代码可能会起作用:

代码语言:javascript
复制
[self.tableView beginUpdates];

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

[self.tableView endUpdates];
票数 9
EN

Stack Overflow用户

发布于 2016-02-07 13:59:34

我遇到了这个问题,它是由一个调用reloadRowsAtIndexPath:withRowAnimation:的块和一个调用reloadData的并行线程引起的。崩溃是由于重载reloadRowsAtIndexPaths:withRowAnimation发现了一个空表,尽管我已经正常检查了numberOfRowsInSection & numberOfSections。

我采取的态度是,我并不真的关心它是否会导致异常。作为应用程序的用户,我可以忍受视觉上的损坏,而不是整个应用程序崩溃。

这是我对此的解决方案,我很乐意分享,并欢迎建设性的批评。如果有更好的解决方案,我很想听听?

代码语言:javascript
复制
- (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;
        }
    });
}
票数 4
EN

Stack Overflow用户

发布于 2016-10-08 14:23:46

经过多次尝试,我发现"reloadRowsAtIndexPaths“只能在某些地方使用,如果只改变单元格的内容,而不是插入或删除单元格。不是任何地方都可以使用它,即使你把它包起来

代码语言:javascript
复制
[self beginUpdates];
//reloadRowsAtIndexPaths
[self endUpdates];

我发现可以使用它的地方是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (IBAction) unwindToMealList: (UIStoryboardSegue *) sender

任何来自其他地方的尝试,比如从"viewDidLoad“或"viewDidAppear”调用它,要么不会生效(对于已经加载的单元格,我的意思是,重新加载不会生效),要么会导致异常。

所以试着只在那些地方使用"reloadRowsAtIndexPaths“。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4461490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档