Режим редактирования UITableView - PullRequest
41 голосов
/ 14 мая 2011

У меня есть 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 ]

61 голосов
/ 14 мая 2011

как Аниш указывает на использование

[tableView setEditing: YES animated: YES]; 

Но вам нужно иметь его в viewWillAppear событии просмотра, чтобы оно работало.

10 голосов
/ 14 мая 2011

попробуйте это ...

[tableView setEditing: YES animated: YES];
7 голосов
/ 02 сентября 2014

В 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

Примечание. И для множественного набора выбора Выбор и редактирование стиля «Несколько» из инспектора атрибутов

2 голосов
/ 09 июня 2016

Чтобы загрузить tableView в режиме редактирования, вы должны вызвать setEditing(true, animated: false) в viewDidLoad().

Если ваш контроллер представления является подклассом UITableViewController, изменять не нужно, просто сделайте вышеуказанный вызов. В противном случае, если ваш контроллер представления является подклассом UIViewController, то вы должны сделать вызов следующим образом: tableView.setEditing(true, animated: true).

Протестировано с Swift 2.2.

0 голосов
/ 09 июня 2016

[self.tableView setEditing:! Self.tableView.isEditing animated: YES];

...