У меня есть элемент BarButton
и имя его Edit
, когда он находится в режиме редактирования, и Done
, когда это делается с помощью редактирования.Поэтому, когда пользователь нажимает на правку, выполняется метод editpress
(см. Код).
При этом, commitEditingStyle
, а также ячейка и запись (из массива, который используется для добавления данных вячейка).
Проблема: Когда я нажимаю на кнопку удаления, ячейка удаляется из таблицы правильно.Но не из массива, который используется для загрузки ячеек.Когда я помещаю точку отладки в commitEditingStyle
, он выполняет этот код только тогда, когда я ПЕРВЫЙ нажимаю на кнопку удаления.Если я удаляю записи для 2-го, 3-го и т. Д., Этот блок кода не выполняется.Почему это ?
Я не добавил [self.tableView setEditing:YES animated:YES];
запись в commitEditingStyle
.Это недостаток.
примечание: я не думаю, что моя логика удаления из массива неверна, потому что та же логика работает в других программах (гарантировано, и поэтому я не опубликовал ее)
-(void) editpress:(id)sender{
UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem;
if (!self.tableView.editing) {
[self.tableView setEditing:YES animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButDone = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButDone;
}
else {
[self.tableView setEditing:NO animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButEdit = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButEdit;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
// I write the logic to delete the record from the array and the table here.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
[self.tableView endUpdates];
}
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
}
}