UITableView в режиме редактирования
Я 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;
}
но не повезло. Что мне делать?
5 ответов
as Аниш указал на использование
[tableView setEditing: YES animated: YES];
но вы должны иметь его в viewWillAppear
просмотр события, чтобы заставить его работать.
в 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];
}
}
для MultipleSelection строки
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
Примечание: существует три стиля Editiing как ниже
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
Примечание: и для множественного выбора установите стиль выбора и редактирования в несколько из инспектора атрибутов
чтобы загрузить tableView в режиме редактирования, вы должны позвонить setEditing(true, animated: false)
на viewDidLoad()
.
если ваш контроллер вида является подклассом UITableViewController
нет необходимости менять, просто сделайте вышеуказанный вызов. В противном случае, если ваш контроллер представления является подклассом UIViewController, вы должны сделать вызов следующим образом: tableView.setEditing(true, animated: true)
.
протестировано с Swift 2.2.