【发布时间】:2018-07-07 00:13:02
【问题描述】:
我遇到了 Crashlytics 提出的以下问题:
[__NSArrayM objectAtIndexedSubscript:]: index 5 beyond bounds for empty array
-TopicListViewController tableView:cellForRowAtIndexPath:]
使用indexPath.row 访问数据源时。
我们有一些异步数据更新来更新数据源,并且该变量是非原子的。
是否有可能在更新数据源时调用cellForRowAtIndexPath?因此导致访问不再存在的索引?
可能是因为变量是非原子的吗?
以下是相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row > [self.tableData count] - 1 || ![self.tableData isValidArray]) {
return nil; //Some protection to prevent this issue...
}
TopicCell * cell = (TopicCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.delegate = self;
NSDictionary * data = nil;
if (self.we_isSearching) {
data = self.we_searchResult[indexPath.row];
} else {
data = [self.tableData objectAtIndex:indexPath.row]; //Crashes here
}
【问题讨论】:
-
用
numberOfRowsInSection方法和cellForRowAt方法的其余部分更新您的问题。 -
它清楚地表明您的
numberOfRowsInSection大于您在cellForRowAtIndexPath中访问的数组的大小,因此它超出了索引并抛出错误。 -
用'numberOfRowsInSection'方法更新你的代码
标签: ios objective-c uitableview