UITableView:在更新错误后从NSInternalInconsistencyException恢复?

Dav*_*Liu 13 iphone cocoa-touch uitableview uikit

所以我通过插入/删除/重新加载行来更新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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,一旦它遇到该异常,reloadData似乎无法正常工作.有没有其他方法可以将UITableView基本上重置为工作状态?

cob*_*bal 4

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

  • 是的,那将是非常理想的。我并不是有意讽刺,但理想情况下一切都会顺利进行,不会出现任何问题。不幸的是,这种情况很少发生。在生产代码中,我更希望它安全地失败,而不是当着用户的面让应用程序崩溃。 (4认同)
  • @David 好吧,如果你绝对决心康复的话。我会捕获异常,然后重新创建表视图。要知道,这几乎肯定会泄漏一些内存,并可能使其他东西处于不良状态。 (3认同)