为什么UITableView commitEditing不起作用?

时间:2016-05-17 12:32:56

标签: ios swift uitableview

这是我的代码:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        print("1")
        let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (rowAction: UITableViewRowAction, indexPath:NSIndexPath) -> Void in
            print("clicking1")
            if let selfiePublicID = self.selfiePublicID {
                print("clicking2")
                let URL = "\(self.properties.host)\(self.properties.addComment)\(selfiePublicID)/action/2"
                print(URL)

                self.userParameters.profileParameters["value"] = self.comments[indexPath.row].commentText

                Alamofire.request(.DELETE, URL, parameters: self.userParameters.profileParameters, encoding: .JSON).validate().responseJSON { response in
                    do {
                        let json = JSON(data: response.data!)
                        print(json)

                        self.comments.removeAtIndex(indexPath.row)
                        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                    }
                }
            }
        }

        deleteAction.backgroundColor = UIColor.redColor()
    }
}

它有效,但现在却没有,我不知道为什么。在日志中我得到1,但后来我没有得到clicking1clicking2

那里有什么问题?

2 个答案:

答案 0 :(得分:0)

更改您的代码,如下所示

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
     print("clicking1")
        if let selfiePublicID = self.selfiePublicID {
            print("clicking2")
            let URL = "\(self.properties.host)\(self.properties.addComment)\(selfiePublicID)/action/2"
            print(URL)

            self.userParameters.profileParameters["value"] = self.comments[indexPath.row].commentText

            Alamofire.request(.DELETE, URL, parameters: self.userParameters.profileParameters, encoding: .JSON).validate().responseJSON { response in
                do {
                    let json = JSON(data: response.data!)
                    print(json)

                    self.comments.removeAtIndex(indexPath.row)
                    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                }
            }
        }
    }
}

希望这会运行。

答案 1 :(得分:0)

当用户滑动时需要自定义操作时,将使用UITableViewRowAction。

目前,您拥有动作处理程序中的所有删除代码,该动作处理程序从未执行,因为动作未显示。 对于这种特殊情况,您不需要操作,只需将其更新为

即可
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        print("1")
        print("clicking1")
        if let selfiePublicID = self.selfiePublicID {
            print("clicking2")
            let URL = "\(self.properties.host)\(self.properties.addComment)\(selfiePublicID)/action/2"
            print(URL)

            self.userParameters.profileParameters["value"] = self.comments[indexPath.row].commentText

            Alamofire.request(.DELETE, URL, parameters: self.userParameters.profileParameters, encoding: .JSON).validate().responseJSON { response in
                do {
                    let json = JSON(data: response.data!)
                    print(json)

                    self.comments.removeAtIndex(indexPath.row)
                    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                }
            }
        }
   }
}