2011-09-04 52 views
2

我已经有UITableView已经编程创建。我现在试图添加使用编辑模式删除行的功能。iOS“刷卡删除”不改变编辑按钮状态

我添加了这样的默认方式:

self.navigationItem.leftBarButtonItem = self.editButtonItem; 

当你轻点按钮,进入编辑模式,按钮变为“完成”按钮。

但是,当我“滑动删除”行时,只显示删除按钮,现有编辑按钮不会更改为“完成”。

有什么我需要做额外的,因为我以编程方式创建表视图?

下面是我的实现代码如下代码:

- (void)viewDidLoad 
{ 

    self.voucherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 36, 320, self.view.frame.size.height - 36) style:UITableViewStylePlain]; 
    self.voucherTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    self.voucherTableView.delegate = self; 
    self.voucherTableView.dataSource = self; 
    [self.view addSubview:self.voucherTableView]; 

    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

} 

// Default table view data source methods go here 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [self.voucherTableView setEditing:editing animated:animated]; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Commit the delete 
    } 
} 
+0

我对你的问题有点困惑。通常使用左侧的删除按钮或通过滑动/滑动删除来完成删除。无论哪种情况,你都应该得到一个“删除”按钮来确认。 “完成”按钮与删除无关,只是退出编辑模式。查看tableview的完整设置也很有帮助。 –

+0

感谢您的评论。对于这两种类型的编辑,我都得到了“删除”按钮,但是当您执行“幻灯片删除”时,原始的“编辑”按钮不会更改为“完成”。我已将我的代码添加到原始问题中。 –

+0

好多了! –

回答

3

这是我的答案:该按钮不会更改为“完成”,因为要删除的幻灯片实际上不必在“编辑”模式下完成,并且不会让您进入编辑模式。

如果你希望当你执行删除时,你可以强制它进入编辑模式,不知道你为什么要这样做?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [tableView setEditing:YES animated:YES]; 
     // Commit the delete 
    } 
} 
+0

好的,我没有意识到这并不意味着。我认为我必须假定,因为在“消息”应用程序中,如果您“幻灯片删除”它将更改编辑按钮完成,显然他们必须手动执行此操作。谢谢你的帮助。 –

+0

是的,它有点不一致。很高兴帮助:) –

+0

Apple的文档为'-tableView:commitEditingStyle:forRowAtIndexPath:'特别说**“你不应该在这个方法的实现中调用setEditing:animated:如果由于某种原因你必须在延迟后调用它通过使用performSelector:withObject:afterDelay:方法。“** –

0

@Scrooby:编辑按钮标题和状态也不会改变为“完成”

这是因为“编辑”按钮,点击触发编辑模式的tableview,但是tableview的编辑模式不会触发事件来将按钮标题改为“完成”。

假设编辑按钮单击(事件A)&的TableView在编辑模式(事件B)

事件A指事件B,但事件B并不意味着事件A.

希望你得到它现在。这是一个简单的逻辑。

希望这可以帮助你。

0

尝试将[super setEditing:editing animated:animated];改为放在方法的末尾。它应该工作。

1

的新功能,电流的iOS 8

只要你使用self.editingItem它现在应该对自己的正确更新。实际上,-tableView:commitEditingStyle:forRowAtIndexPath:的Apple文档现在特别指出:

您不应该在此方法的实现中调用-setEditing:animated:。如果出于某种原因,您必须在延迟后使用-performSelector:withObject:afterDelay:方法调用它。

这是从其他答案中提到的上一个功能的改变。

需要注意的一点是,截至iOS 8.1。2,如果您未在-tableView:commitEditingStyle:forRowAtIndexPath:中调用-deleteRowsAtIndexPaths:withRowAnimation:(例如,在更新数据模型之后调用[self.tableView reloadData]),编辑项将停留在编辑状态。