#iphone #objective-c #xcode
#iPhone #objective-c #xcode
Вопрос:
Мне нужно удалить строку из TableView, и tableview должен обновиться, как я могу это запрограммировать?
моя работа до сих пор;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[tableView endUpdates];
[tableView beginUpdates];
///??????????
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
Моя таблица заполняется с помощью NSArray
called peopleList
, так как я могу удалить запись и обновить представление таблицы?
Ответ №1:
Вам не нужен первый endUpdates
вызов.
Между beginUpdates
и endUpdates
вы также должны удалить объект из своего peopleList
массива, чтобы и в табличном представлении, и в массиве было на 1 элемент меньше при вызове endUpdates
. В остальном все должно работать нормально.
Комментарии:
1. ТАК это что-то вроде этого??? [TableView beginUpdates]; //УДАЛИТЬ ИЗ СПИСКА [TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [TableView endUpdates];
2. Я также получаю это исключение
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) 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, 1 deleted).'
какой-либо подсказки?3. Вам нужно удалить объект, из
peopleList
которого вы записалиDELETE FROM LIST
, чтобы исправить это исключение.
Ответ №2:
Я бы рекомендовал использовать NSMutableArray в качестве хранилища вместо NSArray.
Только что обновил ваше хранилище — в случае NSMutableArray (вместо упомянутого вами NSArray) вам просто нужно вызвать removeObjectAtIndex перед вызовом removeObjectsAtIndex.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
...
// Delete the row from the data source
NSLog(@"delete section: %d rol: %d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]);
[_items removeObjectAtIndex:[indexPath indexAtPosition:1]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
...
}
...
Ответ №3:
В блоке обновления «Начало / конец» вашего табличного представления вы захотите скопировать свой список пользователей в изменяемый массив, удалить запись, а затем установить список пользователей как неизменяемую копию измененного массива.
[tableView beginUpdates];
// Sending -mutableCopy to an NSArray returns an NSMutableArray
NSMutableArray *peopleListCopy = [self.peopleList mutableCopy];
// Delete the appropriate object
[peopleListCopy removeObjectAtIndex:indexPath.row];
// Sending -copy to an NSMutableArray returns an immutable NSArray.
// Autoreleasing because the setter for peopleList will retain the array.
// -autorelease is unnecessary if you're using Automatic Reference Counting.
self.peopleList = [[peopleListCopy copy] autorelease];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];