Когда я прокручиваю свой 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;
}