2016-08-17 4 views
2

セルをクリックして、編集アクションを呼び出したいとします。しかし、私は手動でスワイプを呼び出す実装する方法がわかりません。私はこの答えで解決策があると思いますが、私はかなり客観C. https://stackoverflow.com/a/29839673/5737856swiftで手作業でeditActionsForRowAtIndexPathを呼び出します。

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

関連する問題