In iOS 10 if you want to add a left swipe action to your UITableViewCell you need either to implement the functionality yourself using gesture recognizers, or add to your project an already existing library that implements that functionality.
In iOS 11 things look better, since they added a couple of functions to UITableViewDelegate to exactly support that functionality out of the box.
Setting up our sample project
For starters we need to download XCode 9 beta from the apple site. Then we create a single page application, and add some code to display a simple UITableView with some hardcoded data to serve as testing.
Our View Controller:
lass ViewController: UIViewController { var datasource:[String] = ["Buy groceries", "Visit dentist", "Send invitations", "Do christmas shopping" ] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Extend the UITableViewDelegate, and UITableViewDataSource
extension ViewController: UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return datasource.count } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TodoCell", for: indexPath) cell.textLabel?.text = datasource [indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let item = datasource[indexPath.row] print(item) } }
Add left/right swipe actions to the UITableViewCell
Now we proceed to add the actions. They are different from iOS 10: we do not have a single function editActionsForRowAt, instead we have two actions: one for the actions corresponding to the left swipe, and one for the right swipe (or trailing, and leading as Apple prefers to call them):
- leadingSwipeActionsConfigurationForRowAt
- trailingSwipeActionsConfigurationForRowAt
So to add our new actions we need to implement those functions:
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]) }
As you can see the code is quite similar to the one we use in iOS 10. And here it is the outcome:
That’s all. I hope it was of help 🙂
If you found this post useful you can subscribe to my blog so you can be notified of new posts.
September 28, 2017 at 10:19 pm
How do you get the cells to “close” or snap back automatically within these functions?
LikeLike
October 1, 2017 at 10:29 am
Uhm, I checked the Apple documentation, and I could not find an out of the box way to configure that behavior.
I guess it could be implement using gesture recognizers, though. You could also check out this library https://github.com/SwipeCellKit/SwipeCellKit
At this moment I do not have an iOS computer, so I find myself unable to further investigate this topic.
Sorry I cannot be of more help.
LikeLike
November 15, 2017 at 7:46 am
Hey, have a question, Can we implement the same swipe feature of IOS11 using Objective-C
LikeLike
November 15, 2017 at 9:02 pm
I’d say so.
Have a look at this links where there are samples of objective C code using the leading, and trailing actions:
https://stackoverflow.com/questions/46371146/uitableview-swipe-delete-ios-11-does-not-refresh-reload-data
https://stackoverflow.com/questions/46874524/setting-a-pattern-color-as-backgroundcolor-of-uitableviewrowaction-is-no-longer
LikeLike
January 10, 2019 at 5:46 am
Is there a way to reset the swipe action
i.e after swiping it goes to the normal state
LikeLike
January 10, 2019 at 9:15 pm
Unluckly I do not have a iOS computer anymore, so at this time I cannot experiment with the Swift code to try to find the answer to your question.
Still the question you ask it seems to be a recurrent subject on different iOS developing forums: you could, for example, try the solution proposed in this post https://forums.developer.apple.com/thread/80202
Good luck 🙂
LikeLike
March 5, 2019 at 1:30 am
Good tutorial, but everything code-wise under “Extend the UITableViewDelegate, and UITableViewDataSource” need to be checked for any HTML stuff (like “” and such)
LikeLike