Reputation: 1230
I implemented the swipe from right to left to delete in my tableview using simple apple code, but I also want to add my own swipe from left to right for another button. Basically, I want the same functionality as with swipe to delete, but I will have a custom button, and the custom swipe will be from opposite side while swipe to delete will remain functional.
To make it intuitive, I tried using UIPanGestureRecognizer
, so that a user can see the cell moving while he is swiping (just like the swipe to delete). However, I don't know how to move the actual cell, and additionally, how to add a button below it.
I think I browsed the whole internet and I couldn't find it. Do you have any suggestions?
Upvotes: 1
Views: 2139
Reputation: 581
Gami's answer is the right one to upvote. I'm just adding this below in objective-c to make it clearer to beginners (and those of us who refuse to learn Swift syntax :)
Make sure you declare the uitableviewdelegate and have the following methods:
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color
return @[button, button2]; //array with all the buttons you want. 1,2,3, etc...
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; //tableview must be editable or nothing will work...
}
Upvotes: 2
Reputation: 3757
I have created a new library to implement swippable buttons which supports a variety of transitions and expandable buttons like iOS 8 mail app.
https://github.com/MortimerGoro/MGSwipeTableCell
This library is compatible with all the different ways to create a UITableViewCell and its tested on iOS 5, iOS 6, iOS 7 and iOS 8.
Upvotes: 2
Reputation: 5466
Here is a good article to get you started http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/ on the conceptual level.
Besides there are several open source solutions:
(You should judge the code quality)
And this is just a teaser for (hopefully) upcoming functionality of iOS8 https://gist.github.com/steipete/10541433
Upvotes: 2