Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiping right on a UITableViewCell

So i've searched the site for an answer to this question and there are some decent results but nothing recent since Xcode 7 is no longer in beta and swift 2.0 is now the standard.

I've used the following code in order to make it so that a 'swipe left' feature will cause something to happen to a UITableViewCell -

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
    if editingStyle == UITableViewCellEditingStyle.Delete {
        // ...
    }
}

I understand that this is something that Apple now supplied in their API and use of external classes is not needed.

I also understand you can customize the actions that come up from this swipe using this native code:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
    print("more button tapped")
}

Is there any modern native code which would define a 'right swipe' on a UITableViewCell?

like image 772
hnaderi Avatar asked Sep 06 '25 03:09

hnaderi


1 Answers

func tableView(_ tableView: UITableView,
               leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let closeAction = UIContextualAction(style: .normal, title:  "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("OK, marked as Closed")
        success(true)
    })
    closeAction.image = UIImage(named: "tick")
    closeAction.backgroundColor = .purple
    
    return UISwipeActionsConfiguration(actions: [closeAction])
}

func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let modifyAction = UIContextualAction(style: .normal, title:  "Update", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)
    })
    modifyAction.image = UIImage(named: "hammer")
    modifyAction.backgroundColor = .blue
    
    return UISwipeActionsConfiguration(actions: [modifyAction])
}
like image 179
sourab khajuria Avatar answered Sep 07 '25 21:09

sourab khajuria