43

我有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;
 }

但没有运气。我该怎么办?

4

5 回答 5

62

正如Anish指出使用

[tableView setEditing: YES animated: YES]; 

但是您需要在viewWillAppear视图事件中使用它才能使其工作。

于 2011-05-14T12:39:06.470 回答
10

试试这个...

[tableView setEditing: YES animated: YES];
于 2011-05-14T12:37:35.247 回答
7

在 ViewDidLoad 中写入

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];

添加ORDoneRow

- (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

注意:对于从属性检查器中将多个选择集选择和编辑样式设置为多个

于 2014-09-02T11:49:36.547 回答
2

要在编辑模式下加载 tableView,您应该调用setEditing(true, animated: false).viewDidLoad()

如果您的视图控制器是UITableViewController不需要更改的子类,只需进行上述调用即可。否则,如果您的视图控制器是 UIViewController 的子类,那么您应该以这种方式进行调用:tableView.setEditing(true, animated: true).

用 Swift 2.2 测试。

于 2016-06-09T12:53:11.003 回答
0

[self.tableView setEditing:!self.tableView.isEditing Animation:YES];

于 2016-06-09T19:38:21.770 回答