Программа получила сигнал SIGABRT при прокрутке UITableView - PullRequest
0 голосов
/ 09 января 2012

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

Ответы [ 3 ]

1 голос
/ 09 января 2012

Изменение

NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];

до

NSDictionary *rowData = [self.tableData objectAtIndex:indexPath.row];
0 голосов
/ 10 января 2012
  1. Сначала проверьте, получаете ли вы всю ценность вашего NSMutableArray?
  2. Если вы получаете, убедитесь, что ваш номер раздела tableView равен числу NSMutableArray массива или нет?
  3. Если то же самое, то есть проблема перезагрузки, тогда перезагрузите представление таблицы, когдаВы вставляете данные в NSMutableArray.

Я надеюсь, вы найдете среди них свое решение.

0 голосов
/ 09 января 2012

Как уже отмечали несколько, ваш tableIndexFromIndexPath: неверен.В частности, он возвращает NSNotFound, что предполагает использование чего-то вроде indexOfObject: для объекта, которого нет в массиве.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...