簡體   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