Ответ 1
как Аниш указал на использование
[tableView setEditing: YES animated: YES];
Но вам нужно иметь это в событии просмотра viewWillAppear
, чтобы заставить его работать.
У меня есть UITableView
, и я пытаюсь загрузить его по умолчанию в режиме редактирования. Проблема в том, что когда я эту строку table.editing=TRUE;
мои строки исчезают, я реализовал эти методы:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
но не повезло. Что мне делать?
как Аниш указал на использование
[tableView setEditing: YES animated: YES];
Но вам нужно иметь это в событии просмотра viewWillAppear
, чтобы заставить его работать.
попробуйте это...
[tableView setEditing: YES animated: YES];
В ViewDidLoad пишите
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];
addORDoneRow
- (void)addORDoneRows
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[_dbSongsTblView setEditing:NO animated:NO];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[_dbSongsTblView setEditing:YES animated:YES];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
Для множественного выбора строки
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
Примечание. Существует три стиля редактирования ниже
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
Примечание. И для множественного выбора выбора и редактирования стиля для нескольких из инспектора атрибутов
Чтобы загрузить tableView в режиме редактирования, вы должны вызвать setEditing(true, animated: false)
в viewDidLoad()
.
Если ваш контроллер просмотра является подклассом UITableViewController
, нет необходимости изменять его, просто выполните вышеуказанный вызов. В противном случае, если ваш контроллер представления является подклассом UIViewController, вы должны сделать вызов таким образом: tableView.setEditing(true, animated: true)
.
Протестировано с помощью Swift 2.2.
[self.tableView setEditing:! self.tableView.isEditing анимированный: YES];