Как удалить данные из табличного представления? - PullRequest
0 голосов
/ 19 февраля 2011

Я установил панель навигации с помощью кнопки добавления, которая добавляет данные в мое табличное представление.Я также добавил эту строку self.navigationItem.leftBarButtonItem = self.editButtonItem;, чтобы создать кнопку редактирования.Просматривая метод, я замечаю метод - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath.Поэтому я внес изменения ниже, но мое приложение зависает.Как правильно удалить данные из моей таблицы?

// 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.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


        **[myArray removeObjectAtIndex:[indexPath row]];
        [self.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 Ответ

4 голосов
/ 19 февраля 2011

Попробуйте сначала удалить данные из массива, а затем уведомить таблицу, чтобы удалить соответствующие строки.В этой ситуации вызов -reloadData не нужен:

if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [myArray removeObjectAtIndex:[indexPath row]];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];  
}   
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...