2016-05-17 3 views
0

Это мой код:Почему UITableView commitEditing не работает?

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, но позже я не получаю clicking1 и clicking2.

В чем проблема?

ответ

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

Надеется, что это будет работать.

Смежные вопросы