Возникли проблемы при удалении ячейки - PullRequest
1 голос
/ 19 июля 2011

Когда я пытаюсь удалить ячейку, я получаю сбой ...

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-07-19 08:55:53.575 MyTable[477:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'

Ниже мой код

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [aContentArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text =[aDetailTextArray objectAtIndex:indexPath.row];  
    cell.imageView.image = [UIImage imageNamed:@"home-picture.jpg"];

    return cell;
}
  • (недействительно)viewDidLoad {[super viewDidLoad];

    // Раскомментируйте следующую строку, чтобы отобразить кнопку «Редактировать» на панели навигации для этого контроллера представления.self.navigationItem.rightBarButtonItem = self.editButtonItem;
    aContentArray = [[NSMutableArray arrayWithObjects: @ "iPhone", @ "Android", @ "Blackberry", @ "Symbian", nil] retain];
    aDetailTextAr =[[NSMutableArray arrayWithObjects: @ "iPhone от Apple Inc", @ "Android от Google и это с открытым исходным кодом", @ "Blackberry от RIM Research In Motion", @ "Symbian используется от Nokia", ноль] сохранить];

}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [self.aDetailTextArray removeObjectAtIndex:indexPath.row];
        [self.aContentArray removeObjectAtIndex:indexPath.row];

        [tableView beginUpdates];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];

        [tableView reloadData];

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}

Пожалуйста, помогите мне и спасибо

1 Ответ

0 голосов
/ 19 июля 2011

Проблема в том, что вы удаляете строку, но не удаляете эти данные из своего источника данных (прочитайте сообщение об ошибке еще раз, и вы поймете, что я имею в виду). Не видя, как реализован ваш источник данных, трудно дать точный ответ, но в качестве демонстрации, допустим, вы храните содержимое табличного представления в NSMutableArray с именем items.

[tableView beginUpdates];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[items removeObjectAtIndex:indexpath.row]:
[tableView endUpdates];
//no need to call reloaddata

Опять же, точный метод будет зависеть от того, как заполняется ваша таблица. Выше приведен только пример одного конкретного случая.

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