How to create a "more" button when user swipe a cell in table view (like mail app in ios 7)
I have been looking for this information both here and in the Cocoa Touch forum, but I cannot seem to find the answer and I am hoping someone smarter than myself can give me a solution.
I would like that when the user swipes a table view cell, to display more than one editing button (he default is the delete button). In the Mail app for iOS 7 you can swipe to delete, but there is a "MORE" button that shows up.
Swift 4 & iOs 11+
There is an amazing library called
SwipeCellKit
, it should gain more acknowledgement. In my opinion it is cooler thanMGSwipeTableCell
. The latter doesn't completely replicate the behavior of the Mail app's cells whereasSwipeCellKit
does. Have a lookI used tableViewCell to show multiple data, after swipe () right to left on a cell it will show two buttons Approve And reject, there are two methods, the first one is ApproveFunc which takes one argument, and the another one is RejectFunc which also takes one argument.
You need to subclass
UITableViewCell
and subclass methodwillTransitionToState:(UITableViewCellStateMask)state
which is called whenever user swipes the cell. Thestate
flags will let you know if the Delete button is showing, and show/hide your More button there.Unfortunately this method gives you neither the width of the Delete button nor the animation time. So you need to observer & hard-code your More button's frame and animation time into your code (I personally think Apple needs to do something about this).
As of iOS 11 this is publicly available in
UITableViewDelegate
. Here's some sample code:Also available:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;
Docs: https://developer.apple.com/documentation/uikit/uitableviewdelegate/2902367-tableview?language=objc
I was looking to add the same functionality to my app, and after going through so many different tutorials (raywenderlich being the best DIY solution), I found out that Apple has its own
UITableViewRowAction
class, which is very handy.You have to change the Tableview's boilerpoint method to this:
You can find out more about this on This Site. Apple's own documentation is really useful for changing the background colour:
If you want to change the font of the button, it's a bit more tricky. I've seen another post on SO. For the sake of providing the code as well as the link, here's the code they used there. You'd have to change the appearance of the button. You'd have to make a specific reference to tableviewcell, otherwise you'd change the button's appearance throughout your app (I didn't want that, but you might, I don't know :) )
Objective C:
Swift:
This is the easiest, and most stream-lined version IMHO. Hope it helps.
Update: Here's the Swift 3.0 version: