iOS在UITableView上从左侧滑动,同时保持滑动以删除

时间:2014-05-21 16:49:50

标签: ios objective-c uitableview uipangesturerecognizer

我使用简单的苹果代码在我的tableview中实现了从右到左的滑动删除,但我还想从左到右添加我自己的滑动以获取另一个按钮。基本上,我想要与滑动删除相同的功能,但我会有一个自定义按钮,自定义滑动将从对面,而滑动删除将保持功能。

为了使其更直观,我尝试使用UIPanGestureRecognizer,以便用户可以在刷卡时看到单元格移动(就像要删除的滑动一样)。但是,我不知道如何移动实际单元格,另外,如何在其下方添加按钮。

我想我浏览了整个互联网,但我找不到它。你有什么建议吗?

3 个答案:

答案 0 :(得分:2)

这是一篇很好的文章,可以帮助您从概念层面开始http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/

此外还有几个开源解决方案:

(您应该判断代码质量)

这只是(希望)即将推出的iOS8 https://gist.github.com/steipete/10541433

功能的预告片

答案 1 :(得分:2)

我创建了一个新的库来实现可交换按钮,它支持各种转换和可扩展按钮,如iOS 8邮件应用程序。

https://github.com/MortimerGoro/MGSwipeTableCell

该库兼容所有不同的创建UITableViewCell的方法,并在iOS 5,iOS 6,iOS 7和iOS 8上进行测试。

enter image description here

答案 2 :(得分:2)

加米的回答是正确的回答。我只是在objective-c中添加以下内容,以使初学者(以及那些拒绝学习Swift语法的人)更清楚:)

确保声明uitableviewdelegate并使用以下方法:

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