简体   繁体   中英

Why UITableView commitEditing does not work?

This is my code:

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

it worked, but now it does not and I do not know why. In the log I get 1 , but later I do not get clicking1 and clicking2 .

What is the problem there?

Change your code like below,

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

Hope this will run.

UITableViewRowAction are to be used when you need custom actions when user swipes.

currently you have all the delete code in the action handler which never executes since the action isnt shown ever. For this particular case, you dont need the action and can just update it to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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