【问题标题】:UITableView: Recovering from NSInternalInconsistencyException after bad updates?UITableView:更新错误后从 NSInternalInconsistencyException 中恢复?
【发布时间】:2011-03-09 08:40:02
【问题描述】:

所以我通过根据需要插入/删除/重新加载行来更新 tableview,但是,由于我不是 100% 确信 tableview 将始终正确更新,有什么方法可以安全地从一批坏的更新?

现在,我有这个:

    // Try to animate the updates. If something goes wrong, just reloadData.
    @try {
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:deleteArray withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationNone];
        [tableView insertRowsAtIndexPaths:insertArray withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView endUpdates]; 
    }
    @catch (NSException * e) {
        if([[e name] isEqualToString:NSInternalInconsistencyException]){    
            [tableView reloadData];
            NSLog(@"animation failed, just reloading data");
        }
        else {
            @throw e;
        }
    }

但是,一旦遇到该异常,reloadData 似乎就不起作用了。有没有其他方法可以基本上将 UITableView 重置为工作状态?

【问题讨论】:

    标签: iphone cocoa-touch uitableview uikit


    【解决方案1】:

    更理想的是,您应该使用一个数组来支持该表,该数组可以保证执行该表所期望的操作。 UIKit 期望异常是致命的(这符合 Apple 的理念,即异常表明程序员错误。)

    【讨论】:

    • 是的,那将是非常理想的。我的意思并不是要听起来冷嘲热讽,但理想情况下,一切都会好起来的,不会出错。不幸的是,这种情况很少发生。而在生产代码中,我宁愿它安全地失败,而不是让应用程序在用户面前崩溃。
    • @David 好吧,如果你完全准备好恢复的话。我会捕获异常,然后重新创建表视图。知道这几乎肯定会泄漏一些内存,并可能使其他东西处于不良状态。
    猜你喜欢
    • 2011-01-11
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2013-02-14
    • 2016-07-01
    • 1970-01-01
    • 2012-01-28
    • 2017-12-06
    相关资源
    最近更新 更多