當我滾動我的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