ios - 在 UITableView 滑动中添加额外的按钮

标签 ios tableview customization swipe

目前我正在使用 NSFetchedResultsController 来处理表格 View 。 我想知道有没有办法在滑动操作中添加额外的按钮,比如删除按钮?

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

提前致谢。

最佳答案

您的标题具有误导性。 NSFetchedResultsController 与您的最终目标无关。子类化只会产生比必要更多的工作,您要研究的是 UITableViewRowAction。这与新的 UIAlertController 处理起来非常容易且相似,因此如果您已经尝试过 UIAlertController

,您应该对此感到非常舒服

UITableViewRowAction 的简要介绍(直接来自 the docs):

Asks the delegate for the actions to display in response to a swipe in the specified row. (required) Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object. If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.

开始前的几点注意事项:

  • Apple 在 iOS8 中实现了这一点:因此请仔细考虑您的部署目标。如果您严格地为 i0S8 编码,这是替代第 3 方库或创建自定义 UITableView 编辑样式的快速简便的方法。如果您的目标是保持与 iOS7 的兼容性,您的选择将受限于此格式。 旁注这不会在您的代码中导致 fatal error ,iOS7 应用程序不会崩溃,但是,自定义操作根本不会显示。
  • 除了标题之外没有太多定制
  • 这样做的好处是,一切都用 block 来处理

无论如何,要实现自定义 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
}

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

要真正自定义行操作按钮,请遵循以下一般准则:

第 1 步 实现 tableView:editActionsForRowAtIndexPath: 方法

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

如你所见,它是一个NSArray类型的实例方法,所以你必须返回一个数组。

第 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];
}];

}    

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

要自定义行操作背景颜色,只需像按钮中的大多数其他操作一样预先执行它:

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

进一步引用:LINK TO DOCUMENTATION


针对 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]
}

关于ios - 在 UITableView 滑动中添加额外的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27855151/

相关文章:

html - 当您允许用户在您的网站上编辑 HTML 和 CSS 时,您应该采取哪些预防措施?

objective-c - 使用 NSFetchedResultsController 的应用程序因 NSRangeException _PFBatchFaultingArray 而崩溃

ios - 以编程方式更改 Game Center 中的成就顺序

ios - 如何从JSON更新TableView

linux - GNU Emacs 定制 - 无窗口

django - 问题在django admin中扩展了change_form.html

ios - 使用键值观察和 ECSlidingView 库导致​​主线程崩溃

iOS7选择按钮边框效果

ios - 在通讯录更新时收到通知

ios - Swift:在 UITable-View 中加载图像