简体   繁体   中英

iOS swipe from left on UITableView while remaining Swipe to delete

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?

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

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.

在此处输入图片说明

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...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM