在UITableView滑动添加额外的button

目前我正在使用NSFetchedResultsController来处理表格。 我想知道有什么办法来添加像滑动操作中的删除button的其他button?

我正在考虑将其子类化。 但是在帮助文档中找不到与我的问题相关的内容。

提前致谢。

您的标题是误导性的。 NSFetchedResultsController与您的最终目标没有关系。 子类只会创build更多的工作,你想要看的是UITableViewRowAction 。 这很容易处理和类似于新的UIAlertController ,所以如果你已经涉足UIAlertController

UITableViewRowAction简要概要(直接来自文档 ):

要求代理响应指定行中的滑动显示的操作。 (必需)当您要为其中一个表格行提供自定义操作时,请使用此方法。 当用户在一行中水平滑动时,表视图将行内容移到一边以显示您的操作。 点击其中一个动作button执行与动作对象一起存储的处理程序块。 如果不实现此方法,则表格视图在用户滑动行时显示标准附件button。

在开始之前,请注意两点:

  • 苹果公司在iOS8中实现了这一点:所以仔细想想你的部署目标。 如果您严格为i0S8编码,则这是第三方库的快速简便替代scheme,或创build自定义UITableView编辑样式。 如果你的目标是保持iOS7的兼容性,你的select将受到这种格式的限制。 SIDE注意这不会导致代码中的致命错误,iOS7应用程序不会崩溃,但是,自定义操作不会显示。
  • 标题之外没有太多的定制
  • 这个优点是,所有的东西都是用块来处理的

无论如何,要实现一个自定义的UITableViewRowAction你必须首先通过调用以下方法来确保你的表是可编辑的:

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { //Obviously, if this returns no, the edit option won't even populate return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //Nothing gets called here if you invoke `tableView:editActionsForRowAtIndexPath:` according to Apple docs so just leave this method blank } 

至less,这是在继续之前需要声明的两种方法。

要实际定制您的行操作button,请遵循以下一般准则:

第1 tableView:editActionsForRowAtIndexPath:实现tableView:editActionsForRowAtIndexPath:方法

 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { } 

正如你所看到的,这是一个NSArraytypes的实例方法,所以你必须返回一个数组。

步骤2添加操作对象以传入数组。 一个例子:

 { UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // Delete something here }]; UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //Do whatever here : just as an example : [self presentUIAlertControllerActionSheet]; }]; } 

您可以更改这些行操作的一些属性,但请参阅文档以获取完整的自定义选项:

要自定义行动的背景颜色 ,只需按照其他button中的其他操作进行预制:

 delete.backgroundColor = [UIColor redColor]; more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1]; //arbitrary color 

第3步如前所述,您必须在实例方法中返回一个数组,以便您的完整代码类似于以下内容:

 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // Delete something here }]; delete.backgroundColor = [UIColor redColor]; UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //Just as an example : [self presentUIAlertControllerActionSheet]; }]; more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1]; return @[delete, more]; //array with all the buttons you want. 1,2,3, etc... } 

进一步参考: 链接到文档


编辑为SWIFT语法

 override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { var delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in //Do something }) delete.backgroundColor = UIColor.redColor() var more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in //Do something }) more.backgroundColor = UIColor.blueColor() return [delete, more] } 

所有的荣誉@soulshined接受答案。

这是它的Swift 3版本:

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .default, title: "Delete") { (action:UITableViewRowAction, indexPath:IndexPath) in print("delete at:\(indexPath)") } delete.backgroundColor = .red let more = UITableViewRowAction(style: .default, title: "More") { (action:UITableViewRowAction, indexPath:IndexPath) in print("more at:\(indexPath)") } more.backgroundColor = .orange return [delete, more] }