当我滚动我的UITableView下来,然后向上滚动时,应用程序崩溃与下面的堆栈:程序接收到的信号SIGABRT向上滚动的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;
}
你的tableIndexFromIndexPath方法做了什么? – Vladimir 2012-01-09 14:40:51
@Malek - 同意代码中的注释,'tableIndexFromIndexPath'看起来像什么? – 2012-01-09 14:41:09
我已经编辑了我的帖子,实际上,在'tableIndexFromIndexPath'方法中,我考虑到从我的UITableView中找不到名称并且返回'NSNotFound'的情况。 – Malloc 2012-01-09 15:05:08