iOS5:如何防止rectForRowAtIndexPath方法的异常

Ale*_*x87 5 exception tableview nsindexpath ios5

例外:

无效索引路径请求rect

码:

CGRect rect = [tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
Run Code Online (Sandbox Code Playgroud)

固定:

if ([tableView numberOfRowsInSection:0] <= i) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

也许存在更好的方式

小智 3

通常 rectForRowAtIndexPath 可以按照文档状态处理无效的indexPath,但在IOS 7.1中,它不能正确处理无效的indexPath。因此传入 nil 会导致崩溃并收到错误消息:

请求无效索引路径处的 rect ({length = 2, path = 0 - 0})

总之,对于 rectForRowAtIndexPath 手动执行 nil 检查是必要的。