【问题标题】:editActionsForRowAtIndexPath not called, both commit editingStyle and canEditRowAt presenteditActionsForRowAtIndexPath 未调用,目前都提交了 editingStyle 和 canEditRowAt
【发布时间】:2017-02-28 12:44:13
【问题描述】:

我正在尝试向我的表格视图行添加第二个滑动按钮,但是当我滑动一行时,editActionsForRowAtIndexPath 没有被调用。尽管我的班级都声明了

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

    return true
}

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

}

当我滑动时这两个都会被调用,因为我还有一个常规的删除按钮。

我的editActionsForRowAtIndexPath函数是这样写的:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewRowAction? {

    print("triggered!")

    let more = UITableViewRowAction(style: .default, title: "More") { action, index in
        print("more button tapped")
    }
    more.backgroundColor = UIColor.blue

    return more
}

提前致谢!

【问题讨论】:

  • 你的tableViewdelegate属性设置了吗?
  • 我遇到了同样的问题,@Aaron 的回答对我有用。我不小心从班级中删除了 tableView 的委托。
  • 如果其他人因其他原因遇到此问题,请注意此委托调用是在用户实际滑动 tableView 行时进行的,而不是在调用 tableView reloadData 时进行。确保没有任何东西覆盖 tableView 以防止滑动发生。

标签: swift tableview ios10 tableviewcell


【解决方案1】:

您没有为这个特定方法使用更新的 Swift 3 API。委托方法应该写成:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    print("triggered!")

    let more = UITableViewRowAction(style: .default, title: "More") { action, index in
        print("more button tapped")
    }
    more.backgroundColor = UIColor.blue

    return [more]
}

【讨论】:

  • 你已经把我的错误说清楚了:你的代码不起作用,但它很接近:我的返回值中缺少括号,[更多]。想解释一下为什么没有括号不起作用?
  • @rodrigochousal more 是单个动作,而 [more] 是一组动作。
猜你喜欢
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
相关资源
最近更新 更多