2016-05-16 143 views
0

我有一個編輯按鈕,在導航欄這已被重寫,以顯示選項,而是完成了編輯,並完成一個tableviewcontroller ..定製EditAction按鈕不觸發事件

在我的viewDidLoad函數我有。 ..

// Display an Edit button in the navigation bar for this view controller. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem() 
    // change initial text from Edit to Options 
    self.navigationItem.leftBarButtonItem!.title = "Options" 

我也有以下...

override func setEditing (editing:Bool, animated:Bool) 
{ 
    //set different text for Edit and Done 
    super.setEditing(editing,animated:animated) 
    if(self.editing) 
    { 
     self.editButtonItem().title = "Done" 
    }else 
    { 
     self.editButtonItem().title = "Options" 
    } 
} 

爲了完整,這是我的編輯動作代碼。

// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

    switch editingStyle { 

    case .Delete: 
     let context = fetchedResultsController.managedObjectContext 
     if indexPath.row == 0 { 
      //this is the top (first row) 
      // Deleting without warning 
      let indexPathToDelete = NSIndexPath(forRow: 0, inSection: 0) 
      let objectToDelete = fetchedResultsController.objectAtIndexPath(indexPathToDelete) as! NSManagedObject 
      context.deleteObject(objectToDelete) 

      do { 
       try context.save() 
      } catch { 
       print(error) 
      } 

     } else { 
      //we are deleted a row that is not the top row 
      // we need to give a warning and if acknowledged then delele all rows from the selected row and all rows above it 

      let alertController = UIAlertController(title: nil, message: "Are you sure? This will remove this and all logs above it.", preferredStyle: .Alert) 
      let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in 

      } 
      alertController.addAction(cancelAction) 
      let deleteAction = UIAlertAction(title: "Delete", style: .Default) { (action) in 

       for deleteindex in 0 ... indexPath.row { 
        let deleteIndexPath = NSIndexPath(forRow: deleteindex, inSection: 0) 
        let objectToDelete = self.fetchedResultsController.objectAtIndexPath(deleteIndexPath) as! NSManagedObject 
        context.deleteObject(objectToDelete) 
       } 

       do { 
        try context.save() 

       } catch { 
        print(error) 
       } 
      } 
      alertController.addAction(deleteAction) 

      // Dispatch on the main thread 
      dispatch_async(dispatch_get_main_queue()) { 
       self.presentViewController(alertController, animated: true, completion:nil) 
      } 

     } 
     break; 

    default : 
     return 
    } 

} 

這工作得很好......

現在,現在我已經添加了自定義編輯行爲......平時刪除+出口通過將此代碼...

//Override edit actions 
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in 
     // delete item at indexPath 
    } 

    let export = UITableViewRowAction(style: .Normal, title: "Export") { (action, indexPath) in 
     // export item at indexPath 
    } 

    export.backgroundColor = UIColor.blueColor() 

    return [delete, export] 
} 

現在當處於編輯模式時,每個單元格現在有兩個選項「導出」和「刪除」,但在按下時不會執行任

我有一個斷點在行case。刪除,但它沒有被擊中。

回答

1

你將不得不把你刪除的代碼到它的行動:

// Override edit actions 
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in 
     // You code for Deletion here... 

    } 
+0

信不信由你,因爲我是構建這一閃念的問題...所以我並不需要覆蓋FUNC的tableView(的tableView :UITableView,commitEditingStyle editingStyle:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){}了嗎? – Mych

+0

的權利,你會留下它空的移動刪除代碼到「刪除」行動塊 – cubycode