【问题标题】:Swift editActionsForRowAtIndexPathSwift editActionsForRowAtIndexPath
【发布时间】:2014-10-05 19:27:54
【问题描述】:

我正在尝试在我的表格视图中添加一些编辑操作。但是,我无法弄清楚要为 UITableViewRowAction 输入什么!。它在代码中表示为IDK

func Done(UITableViewRowAction!,
    NSIndexPath!) -> Void {

}

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

    let complete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done", handler: Done(_IDK_, indexPath))

    let arrayofactions: Array = [complete]

    return arrayofactions
}

【问题讨论】:

    标签: ios swift tableview


    【解决方案1】:
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
            println("delete")
        }
        let done = UITableViewRowAction(style: .Default, title: "Done") { action, index in
            println("done")
        }
        return [delete, done]
    }
    

    【讨论】:

      【解决方案2】:

      在这种情况下,闭包对我有用。

      func Done(UITableViewRowAction!,
      NSIndexPath!) -> Void {
      
      }
      
      override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath:      NSIndexPath) -> [AnyObject]? {
      
      let complete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done", handler: { (action, indexPath.row) -> Void in 
       'handle your action here'
      
      })
      
      let arrayofactions: Array = [complete]
      
      return arrayofactions
      }
      

      我找到了一篇关于此的博客文章,它对我有用。

      【讨论】:

      • 这里是该博客的链接link
      • 此外,您可以使用 Swift 尾随闭包语法来获得更简洁的代码: let complete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done") { (action, indexPath.row) -> '在此处处理您的操作' } 无效
      • 上面的代码只在我删除“.row”时为我编译,如下所示: UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done") { (action, indexPath) -> Void在'在这里处理你的行动'}
      【解决方案3】:

      对于 Swift 2.2

      我不得不这样更新

      func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
      {
         return true
      }
      
      func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
      {
          let delete = UITableViewRowAction(style: .Normal, title: "Delete") 
          { action, index in
              print("delete")
          }
          let done = UITableViewRowAction(style: .Default, title: "Done") 
          { action, index in
              print("done")
          }
          return [delete, done]
      }
      

      【讨论】:

        【解决方案4】:

        添加这个方法

        - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
        {
        // All tasks are handled by blocks defined in editActionsForRowAtIndexPath, however iOS8 requires this method to enable editing
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-23
          • 2017-02-28
          相关资源
          最近更新 更多