繁体   English   中英

为什么UITableView commitEditing不起作用?

[英]Why UITableView commitEditing does not work?

这是我的代码:

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

那里是什么问题?

如下更改代码,

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)
                }
            }
        }
    }
}

希望这会运行。

当用户滑动时需要自定义操作时,将使用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)
                }
            }
        }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM