【问题标题】:add additional buttons in UITableView swipe在 UITableView 滑动中添加其他按钮
【发布时间】:2015-03-07 10:54:38
【问题描述】:

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

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

提前致谢。

【问题讨论】:

    标签: ios tableview customization swipe


    【解决方案1】:

    您的标题具有误导性。 NSFetchedResultsController 与您的最终目标无关。子类化只会创建不必要的工作,您要查看的是UITableViewRowAction。这很容易处理并且与新的UIAlertController 类似,所以如果你已经涉足UIAlertController,你应该对此感到非常舒服

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

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

    开始前的几点说明:

    • Apple 在 iOS8 中实现了这一点:因此请仔细考虑您的部署目标。如果您严格为 i0S8 编码,这是一个快速简便的替代 3rd 方库或创建自定义 UITableView 编辑样式的方法。如果您的目标是保持与 iOS7 的兼容性,那么您的选择会受到这种格式的限制。 旁注这不会在您的代码中导致致命错误,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
    }
    

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

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

    第一步实现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]
    }
    

    【讨论】:

    • 感谢您对编辑标题的建议。但事实上,我正在尝试定制 NSFetchedResultsController 以启用额外的 btns。您的回答提供了解决此问题的另一种解决方案,但不是我的初衷。再次感谢您的回答。
    • 也许你可以澄清@AlexWei 我非常愿意编辑我的答案。我感到困惑的原因是 NSFetchedResultsController 只是从核心数据中检索信息以显示在 UITableView 中。操作按钮是 UITableView 的一部分,而不是获取控制器。如果您可以针对您的问题发布参考链接或图片,我可能会更好地理解以帮助您
    • 看来我完全错了。我没有意识到“操作按钮是 UITableView 的一部分而不是获取控制器”。我认为这些按钮属于 fetch 控制器。 :(
    • 我有固定的标题。再次感谢您的慷慨帮助!
    • @AlexWei 没关系。我们都在这里学习和成长!我只是不想让你在没有解决问题的情况下悬而未决。这就是为什么我建议您编辑标题,以便其他有相同问题的人可以找到解决方案。
    【解决方案2】:

    @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]
    }
    

    【讨论】:

    • 感谢您的转换。
    猜你喜欢
    • 2018-06-04
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多