我有一个 iOS 7/8 应用程序。在一个视图中,我有一个具有给定单元格数量的静态 UITableView - 在我的例子中是 17 个。
其中一个单元格包含另一个带有动态单元格的 UITableView。在所描述的情况下,它们是 20。
由于单元格数量(+3)的差异,我得到
NSRangeException',原因:'*** -[__NSArrayI objectAtIndex:]:索引 17 超出范围 [0 .. 16]
当我在动态视图中设置第 18 个单元格时出现异常。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == _dynamicTableView) {
NSLog(@"%lu", (unsigned long)[[_filter types] count]);
return [[_filter types] count];
} else {
return 17;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _dynamicTableView) {
static NSString *cellIdentifier = @"type";
TypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[TypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
NSArray *types = [_filter types];
Type *type = [types objectAtIndex:[indexPath row]];
[cell.nameLabel setText:[type name]];
return cell;
} else {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
当我转到情节提要并将静态单元格的数量增加到 30 个时,一切正常。tableView:numberOfRowsInSection... 方法删除未使用的单元格 - 仅显示 17 个静态单元格和 20 个动态单元格。
我知道我的问题的根源是一个控制器中有两个 UITableView-s 和大量的“if”-s。