滑动以删除并单击“更多”按钮(例如,在iOS 7上的Mail应用程序中)

用户在表格视图中滑动单元格时如何创建“更多”按钮(如ios 7中的邮件应用程序)

我一直在这里和Cocoa Touch论坛上都在寻找这些信息,但是我似乎找不到答案,我希望有一个比我自己更聪明的人给我解决方案。

我希望当用户滑动表格视图单元格时,显示多个编辑按钮(他默认是删除按钮)。在iOS 7的邮件应用中,您可以滑动以删除,但是会出现一个“更多”按钮。


不负相思意
浏览 635回答 3
3回答

慕神8447489

我只是在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...    }

智慧大石

现在可以使用以下公共API来完成此操作:func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {    let moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "More", handler:{action, indexpath in        print("MORE•ACTION");    });    moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);    let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete", handler:{action, indexpath in        print("DELETE•ACTION");    });    return [deleteRowAction, moreRowAction];}
打开App,查看更多内容
随时随地看视频慕课网APP