【问题标题】:I implemented editActionsForRowAtIndexPath and commitEditingStyle but no edit actions appear on the tableViewCell when swiping the cell我实现了 editActionsForRowAtIndexPath 和 commitEditingStyle,但是在滑动单元格时 tableViewCell 上没有出现编辑操作
【发布时间】:2015-06-23 13:23:04
【问题描述】:

我实现了editActionsForRowAtIndexPathcommitEditingStyle 滑动正常,但UITableViewCell 上没有出现编辑操作

我对@9​​87654324@ 和commitEditingStyle 的实现如下:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            //I did some work here
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }


 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        //I did some work here
        tableView.reloadData()
    })


    return [deleteAction]
}

任何帮助将不胜感激

【问题讨论】:

  • 你是否暗示 canEditRowAtIndexPath 返回 YES
  • 是的,我做到了,它在另一个 viewController 中工作,没有实现 canEditRowAtIndexPath

标签: ios swift uitableview tableviewcell uitableviewrowaction


【解决方案1】:

我认为您在这里混合了两种不同的编辑方式。

第一种编辑是旧的UITableViewCellEditingStyle.Delete。新方法是提供您的自定义附件视图。

如果您实现自定义附件视图,则默认删除按钮将不会显示,因此不会被调用。所以你的

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

在我看来,甚至可能不会被调用。

Apple 的文档 For editActionsForRowAtIndexPath 包含以下句子:If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row. 我假设如果你实现了这个方法,标准的附件视图将不会显示。

编辑: 代码示例(更新到 Swift 3 11/17/16)

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

private func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? {
    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in
    })
    return [deleteAction]
}

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

}

编辑 2: 正如 rajagp 指出的那样,如果您只针对 iOS9(或更高版本),则不需要空实现。

【讨论】:

  • 我实现了 editActionsForRowAtIndexPath 但附件视图没有显示
  • 如果我没有实现 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 刷卡不起作用
  • 在 Xcode 7 GM 候选版本上验证 iOS 9 不再需要重写 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 的空实现。因此,如果您将部署目标设置为 iOS9 的基线,则不需要空实现。
  • 我正在使用func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? { let more = UITableViewRowAction(style: .normal, title: "more" , handler: { (action:UITableViewRowAction, indexPath: IndexPath) -> Void in }) return [more] },但没有显示more 按钮,而是默认的delete 按钮
【解决方案2】:

您需要从 UITableview 委托方法中实现 canEditRowAtIndexPath 并返回 true。

【讨论】:

  • 我已经实现了 canEditRowAtIndexPath 但还是不行
  • 问题是 editActions 出现在另一个 ViewController 中,有和没有实现 canEditRowAtIndexPath 但在其他 ViewController 中它们没有出现,但滑动运行良好
  • 另一个控制器是 UIViewController 的子类还是 UITableViewController 的子类或您要继承功能的其他类?
  • 另一个控制器是UIViewController的子类,是tableview的delegate
【解决方案3】:

尝试在删除按钮内添加一些操作

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
    {
        let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let alertView = UIAlertController(title: "Delete Action", message: "", preferredStyle: UIAlertControllerStyle.Alert)
            alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)

        })
        delete.backgroundColor = UIColor.redColor()


        let more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let alertView = UIAlertController(title: "More Action", message: "", preferredStyle: UIAlertControllerStyle.Alert)
            alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
        })
        more.backgroundColor = UIColor.blueColor()

        return [delete, more]
    }

【讨论】:

    【解决方案4】:

    TLDR:

    实现的必要功能: • 提交编辑风格 • canEditRowAt • editActionsForRowAt

    注意:对于editActionsForRowAt,你不能在这里返回一个空数组——它会搞砸所有的单元格。如果您不想允许对其进行编辑操作的特定类型的行,请在 canEditRowAt 中指定该类型的单元格以返回 false。

    【讨论】:

      【解决方案5】:

      很抱歉唤醒了这么一个旧线程,但我在 Swift 3 上实现了它:

      func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
          return .delete
      }
      

      【讨论】:

        【解决方案6】:

        我刚刚复制了显示行操作按钮并运行良好的 ViewController,并替换了滑动时不显示行操作按钮的 ViewController,现在显示了行操作按钮并执行了预期的行为。

        但我不明白这个问题。有人能解释一下吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-22
          相关资源
          最近更新 更多