3

[UITableView reloadData]未在 @catch 块中调用

这是我的代码:

@try {
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
@catch (NSException *exeption) {
    [self.tableView reloadData];
}

有时在 tableview 中插入新行时会出现问题(具体哪个都没有关系),我想处理它。虽然我测试异常引发 @catch 块处理它并且不会发生崩溃,但 reloadData 也没有调用。我还尝试使用perfomSelectorOnMainThread:GCD 在主线程上手动调用 reloadData:

@catch (NSException *exeption) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

但它也没有产生任何效果。有人可以提出一些建议吗?谢谢!

4

1 回答 1

3

In Objective-C, it's not a good idea to catch the exception and keep going. It's better to figure out what's wrong and prevent the exception from happening.

But for whatever reason, you can try -performSelector:withObject:afterDelay:.

[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.0];

Update

-performSelector:withObject:afterDelay: always runs in a later run loop even when the delay is 0.

[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.0];

The effect is to call the -reloadData method in the next run loop. This is useful when there are UI changes are pending in the current run loop.

于 2013-03-21T00:32:44.690 回答