简体   繁体   English

当没有更多要删除的单元格时,如何在 UITableView 中关闭编辑模式?

[英]How to turn off editing mode in UITableView when there are no more cells to delete?

I've tried putting this in various parts of my code, like at the end of commitEditingStyle method, but I can't get it to stop editing mode.我试过把它放在我代码的各个部分,比如在 commitEditingStyle 方法的末尾,但我无法让它停止编辑模式。 Basically, I want to automatically exit editing mode when there are no more cells...基本上,我想在没有更多单元格时自动退出编辑模式......

if ([self.tableView numberOfRowsInSection:0] ==0)
    {
        NSLog(@"this triggers, but doesn't stop editing..");
        self.tableView.editing = NO;
        [self.tableView endEditing:YES];
    }

How about [self setEditing:NO animated:YES] ? [self setEditing:NO animated:YES]怎么样? I suppose to self is an instance of UITableViewController.我想 self 是 UITableViewController 的一个实例。

From the apple docs:来自苹果文档:

Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.

So, calling this within commitEditingStyle is not a great practice.因此,在commitEditingStyle调用它并不是一个好习惯。

如果不只是[self setEditing:NO animated:YES]

After playing around with this, here are the things to know: There are separate setEditing routines for the controller and for the tableView.在玩弄这个之后,这里是需要知道的事情: 控制器和 tableView 有单独的 setEditing 例程。 Make sure to use the one for the controller.确保将其用于控制​​器。 Also, it needs a delay as noted above.此外,它需要如上所述的延迟。 For this, I used Matt's delay function .为此,我使用了Matt 的延迟功能 As an added bonus, one can disable the Edit button when there are no items in your list.作为额外的好处,当您的列表中没有项目时,您可以禁用“编辑”按钮。 The button becomes enabled again when an item is added.添加项目后,该按钮将再次启用。 Code is in Swift5.代码在 Swift5 中。

var someArray: [SomeStruct] = [] {
    didSet {
        if let btn = navigationItem.rightBarButtonItem {
            if someArray.count > 0 {
                btn.isEnabled = true
            } else {
                btn.isEnabled = false   // Needs to respond immediately - don't put in delay
                closeDownEditMode()
            }
        }
    }
}

func delay(_ delay:Double, closure:@escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

func closeDownEditMode() {
    delay(0.1) { [weak self] in
        self?.setEditing(false, animated: true)
    }
}

Swift 5 等效项:

super.setEditing(false, animated: true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在编辑模式下在UITableView中添加额外的单元格? - How to add extra cells in a UITableView in editing mode? 如何在编辑模式下禁用删除,并且在使用UITableViewStyleGrouped时没有表视图单元格插入? - How to disable delete in editing mode and not have table view cells inset when using UITableViewStyleGrouped? iPhone:使用编辑模式在UITableview中添加和删除行 - iPhone:Add & Delete Row in UITableview with editing mode 如何检测UITableView是否处于编辑模式? - How to detect that UITableView is in editing mode? 在UITABLEVIEW的编辑模式下单击删除按钮时,仅删除一行中的文本(而不删除行) - delete only text in a row (but not the row) when delete button is tapped in editing mode of UITABLEVIEW 当 UITableViewCell 处于编辑模式并且我们重新加载 UITableView 时,即使在重新加载后如何保持该特定单元格处于编辑模式 - When a UITableViewCell is in editing mode and we reload the UITableView, how to keep that particular cell in editing mode even after reload 当UITableView处于编辑模式时显示一个UIButton - show a UIButton when the UITableView is in editing mode 当UITableView进入编辑模式时,调整UITableViewCells的大小 - Resizing UITableViewCells when a UITableView enters editing mode 在编辑模式下将 UITableView 与带有子视图/图层的自定义单元格分组 - Grouped UITableView with custom cells with subviews/layers in editing mode 如何使 UITableView 中的单元格不变成蓝色? - How to make cells in UITableView not turn blue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM