2016-05-16 4 views
0

У меня есть tableviewcontroller с кнопкой Edit в навигационной панели Это было переопределен, чтобы показать Параметры и Done вместо Edit и Done ..Настроенные 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] 
} 

и теперь когда в режиме редактирования каждая ячейка теперь имеет два варианта «Экспорт» и «Удалить», но при нажатии ничего не делает.

У меня есть точка останова на линии. Удалите, но не ударяйте.

ответ

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

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