UITableView 编辑模式

44

我有一个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个回答

63

正如Anish所指出的,建议使用

[tableView setEditing: YES animated: YES]; 

但你需要将它放在 viewWillAppear 视图事件中才能使其工作。


1
是的,这个方法可以解决问题,但我的右键消失了。我该怎么办? - user739509
你是指 self.navigationItem.rightBarButtonItem 吗?为什么不尝试在 viewWillAppear 中添加它呢? - Ahmad Kayyali
1
不,我的意思是我有一个按钮,你可以通过它跳转到另一页。 - user739509
它存在于正常模式中,我的意思是附属按钮。 - user739509
1
你在 cellForRowAtIndexPath 委托方法中添加了 [cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;]; 吗? - Ahmad Kayyali
你需要查看这个 editingAccessoryType - Ahmad Kayyali

10

试试这个...

[tableView setEditing: YES animated: YES];

没有运气。我也尝试在设置编辑模式后重新加载数据:[table reloadData]; - user739509
请查看这里:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html - Anish

7
在`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

注意:对于多项选择,请从属性检查器中设置选择和编辑样式为“Multiple”。


有没有必要调用 reloadData?在我的情况下,除了禁用进入/退出编辑模式时的动画之外,没有必要。 - rgkobashi

2
要在编辑模式下加载tableView,您应该在viewDidLoad()中调用setEditing(true, animated: false)。如果您的视图控制器是UITableViewController的子类,则无需更改,只需进行上述调用即可。否则,如果您的视图控制器是UIViewController的子类,则应以以下方式进行调用:tableView.setEditing(true, animated: true)已使用Swift 2.2进行测试。

0

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

这段代码是用于在iOS编程中控制表格视图的编辑模式。它通过设置表格视图的editing属性来切换编辑模式,animated参数则指定了是否需要动画效果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接