UITableView и сбой numberOfRowsInSection - PullRequest
2 голосов
/ 05 июля 2010

У меня проблема при попытке удалить строки из моего UITableView: UITableView получает количество строк из NSMutableArray:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [alertsArray count];
}

Я добавляю объекты в NSMutableArray следующим образом:

- (void)saveButton:(id)sender {
[alertsArray addObject:
[self.tableView reloadData];
}

С помощью этого кода я разрешаю удалять строки:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source.
    [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [alertsArray removeObjectAtIndex:indexPath.row];
    [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 Ответ

1 голос
/ 05 июля 2010

Вы должны сделать только

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source.
    [alertsArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} 

То есть сначала исправьте свою модель, а , затем , позвоните deleteRowsAtIndexPaths:

Соответствующее сообщение об ошибке вы найдете в окне консоли, кстати.

Также, как правило, нет необходимости использовать reloadData здесь, когда вы также не изменили другие вещи.

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