4

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
hnaderi
  • 409
  • 8
  • 16
  • Possible duplicate of [Swipe-able Table View Cell in iOS9 or Swift Guide At Least?](http://stackoverflow.com/questions/32004557/swipe-able-table-view-cell-in-ios9-or-swift-guide-at-least) – Anbu.Karthik Jan 22 '16 at 08:07
  • try custom swipe to delete https://github.com/MortimerGoro/MGSwipeTableCell – NANNAV Jan 22 '16 at 12:46
  • @NANNAV yeah looks like that's the only way to get it done – hnaderi Jan 23 '16 at 01:27
  • @Anbu.Karthik yeah i was hoping there would be some new development from that post but doesn't seem like it – hnaderi Jan 23 '16 at 01:27

3 Answers3

5
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])
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
4

The feature is available on iOS 11.0+ and Mac Catalyst 13.0+. For options on the left use the leading and on the right use the trailing methods.

Just implement the UITableViewDelegate methods and return your UISwipeActionsConfiguration object. You could optionally include an image or just the text as the title of the button.

func tableView(_ tableView: UITableView, 
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

func tableView(_ tableView: UITableView, 
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

Make sure to enable editing in your data source delegate otherwise the swipe options are disabled.

// True for enabling the editing mode
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
naz
  • 1,478
  • 2
  • 21
  • 32
0

No, there Is no such native API as of now.

smnk
  • 471
  • 1
  • 6
  • 25
  • 1
    thank you! it's so strange that they would include native API for adding in values for the 'left swipe' and also strange that they have a clear example of a 'swipe right' in the mail app but not provide it in the API :( – hnaderi Jan 23 '16 at 01:26
  • I can't explain it myself. But I guess it will be coming with one of the next updates, as well as expanding this API by granting Access to subclass those Views, and therefore allowing for better customization with eg. Images – smnk Jan 23 '16 at 06:48