2016-05-16 4 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" 

私もこれはeditアクションのための私のコードで完全を期すため

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

とをカスタム編集アクションを追加しました編集モードでは、各セルに[エクスポート]と[削除]の2つのオプションがありますが、押されても何も実行されません。

私は行のcase.Deleteにブレークポイントを持っていますが、ヒットしていません。あなたは、そのアクションにあなたの削除コードを配置する必要があります

答えて

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

関連する問題