2016-08-17 5 views
2

Я хочу щелкнуть ячейку и вызвать действие редактирования. Но я не уверен, как реализовать вызов swipe вручную. Я думаю, что есть решение в этом ответе, но я не совсем понимаю цель C. https://stackoverflow.com/a/29839673/5737856вызов editActionsForRowAtIndexPath вручную в swift

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // swipe 
    //tableView(tableView, editActionsForRowAtIndexPath: indexPath) or other functions??? 
} 

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return true 
} 

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     print("EDIT") 
    }) 
    editAction.backgroundColor = UIColor.blueColor() 

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     print("DELETE") 
    }) 
    return [deleteAction, editAction] 
} 

ответ

0

Вы можете использовать этот метод делегата, чтобы получить салфетки удалить.

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 

     return UITableViewCellEditingStyle.Delete 

    } 
+0

Я хочу, чтобы ячейка была пробита и показывая кнопку EDIT и кнопку DELETE, нажав на ячейку – Tony

2

Вот способ для этого:

var selectionIndexPath: NSIndexPath? 

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 

    if selectionIndexPath != nil { 

     if selectionIndexPath!.row == indexPath.row { 

      return true 
     } else { 

      return false 
     } 

    } else { 

     return true 
    } 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    selectionIndexPath = indexPath 
    tableV.setEditing(true, animated: true) 
} 

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     print("EDIT") 
    }) 
    editAction.backgroundColor = UIColor.blueColor() 

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     print("DELETE") 
    }) 

    let cancleAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Cancle" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 

     self.tableV.setEditing(false, animated: true) 
    }) 
    cancleAction.backgroundColor = UIColor.lightGrayColor() 
    return [cancleAction, deleteAction, editAction] 
} 

И ваш результат будет:

enter image description here

Надеется, что это поможет.

+1

, есть ли способ показать кнопки удаления и редактирования напрямую или поместить кнопки удаления и редактирования влево? – Tony