当我向下滚动我的UITableView然后向上滚动时,应用程序会崩溃并显示下面的堆栈:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 48]'
我知道它说我正在访问超过UITableView大小的单元格索引,但我无法弄清楚如何修复它。这可能是我的相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CheckedTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];//this line may be the source of the crash
cell.textLabel.text = [rowData objectForKey:kCellTextKey];
if ([[rowData objectForKey:kCellStateKey] boolValue]) {
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
cell.accessoryView = imageView1;
} else {
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
cell.accessoryView = imageView2;
}
return cell;
}
修改:
这是我的tableIndexFromIndexPath
方法实现
- (NSUInteger)tableIndexFromIndexPath:(NSIndexPath *)indexPath {
// Get list of items at selected section
NSArray *listData = [tableContents objectForKey:[sortedKeys objectAtIndex:indexPath.section]];
// Get name of selected row within the section
NSString *textKey = [listData objectAtIndex:indexPath.row];
// Look up that name in the tableData array
for (int i=0; i < [tableData count]; i++) {
NSDictionary *dict = [tableData objectAtIndex:i];
if ([[dict objectForKey:kCellTextKey] isEqualToString:textKey]) {
return i;
}
}
//In case Name was not found
return NSNotFound;
}
答案 0 :(得分:1)
更改
NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];
到
NSDictionary *rowData = [self.tableData objectAtIndex:indexPath.row];
答案 1 :(得分:0)
正如有些人所说,您的tableIndexFromIndexPath:
不正确。具体来说,它正在返回NSNotFound
,这表示您使用类似indexOfObject:
的内容来处理不在数组中的对象。
答案 2 :(得分:0)
NSMutableArray
的全部价值? NSMutableArray
数组的数量?NSMutableArray
时重新加载表格视图。我希望你能找到他们的解决方案。