【问题标题】:UITableView edit mode not showing delete buttonUITableView 编辑模式不显示删除按钮
【发布时间】:2017-09-28 18:21:43
【问题描述】:

我正在从 Apple 复制示例 Food Tracker 应用程序,但在编辑模式下我的 TableView 中的项目不显示左侧删除图标(红色圆形图标)。如果我向左滑动,我会在右侧看到一个删除按钮。我已经下载了 Food Tracker 代码,它可以工作。

我可以看到的区别是示例应用程序实现了 UITableViewController,而我在标准 UIViewController 中有一个 UITableView。我不确定为什么这不起作用。

有一些类似的问题,但它们是旧版本的 Swift / Ios,所以我不确定它们是否仍然相关。

这里是示例应用程序https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1的链接

这里是我最近添加的应该使它工作的函数

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
    //delete the row from the dataSource
        dataSource.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)

    }
    else if editingStyle == .insert {
    //create a new instance and insert the row

    }

}

我将它添加到 ViewDidLoad() 以获取导航栏中的“编辑”按钮

 navigationItem.leftBarButtonItem = editButtonItem

Food Tracker 示例如下所示;

enter image description here

我的左手按钮不见了。我能看到的唯一区别是我在 ViewController 中使用了 TableView。

谢谢

尼克

【问题讨论】:

  • 分享一些代码
  • 如果没有看到您的整个设置,这很难诊断。可能这会发光:stackoverflow.com/questions/3309484/…
  • 添加editButtonItem的动作方法
  • Nirav D - 您能否详细说明需要添加哪些操作方法以及如何添加?
  • @NickS 你已经在leftBarButtonItem设置了editButtonItem,然后显示editButtonItem的action方法,当你按下这个编辑按钮时会调用的方法

标签: ios swift3


【解决方案1】:

无需检查 barButtonItem 的标题,因为点击它已经切换其状态并在编辑参数中传递正确的状态。

此外,在覆盖时,您需要调用 super 的实现,因为缺少总是会显示删除指示符。

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: true)
    tableView.setEditing(editing, animated: true)
}

【讨论】:

    【解决方案2】:

    我想我解决了这个问题。实际上,UITableViewController 正在做一些“魔术”。要在带有 TableView 的普通 UIViewController 中重新创建它,您必须覆盖 SetEditing 函数并根据按钮的标题将其置于编辑模式。

    override func setEditing(_ editing: Bool, animated: Bool) {
    
    
        let status = navigationItem.leftBarButtonItem?.title
    
    
    
        if status == "Edit" {
    
            tableView.isEditing = true
    
            navigationItem.leftBarButtonItem?.title = "Done"
    
            }
    
        else {
    
            tableView.isEditing = false
    
            navigationItem.leftBarButtonItem?.title = "Edit"
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2014-11-18
      相关资源
      最近更新 更多