在UITableView滑动中添加其他按钮 [英] add additional buttons in UITableView swipe

查看:119
本文介绍了在UITableView滑动中添加其他按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



提前致谢。

解决方案

你的标题具有误导性。 NSFetchedResultsController与您的最终目标无关。子类化只会创建超出必要的工作量,您想要查看的是 UITableViewRowAction 。这非常容易处理类似于新的 UIAlertController ,所以如果你已经涉足 UIAlertController ,那么你应该对此非常满意。 / p>

UITableViewRowAction的简要概要(直接来自文档) / code>:


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


开始前的几点说明:




  • Apple在iOS8中实现了这一点:所以请仔细考虑您的部署目标。如果您严格编码i0S8,这是第三方库的快速简便替代方案或创建自定义UITableView编辑样式。如果您的目标是保留iOS7的兼容性,则您的选项受限于此格式。 SIDE NOTE 这不会导致代码出现致命错误,iOS7应用不会崩溃,但是,自定义操作根本无法显示。

  • 没有多少定制超出标题

  • 这个的优点是一切都是用块处理的



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

   - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//显然,如果这返回no,编辑选项甚至不会填充
返回YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//如果你这里没有调用任何东西根据Apple文档调用`tableView:editActionsForRowAtIndexPath:`所以只需将此方法留空
}

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



要实际自定义行动作按钮,请遵循以下一般准则:



第1步实现 tableView:editActionsForRowAtIndexPath:方法

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

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



第2步添加操作对象以传入数组。例如:

  {

UITableViewRowAction * delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@删除handler:^(UITableViewRowAction * action,NSIndexPath * indexPath)
{
//删除这里的东西
}];

UITableViewRowAction * more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@Morehandler:^(UITableViewRowAction * action,NSIndexPath * indexPath)
{
//在此处执行任何操作:举个例子:
[self presentUIAlertControllerActionSheet];
}];

}

您可以更改这些行动作的一些属性,但是查看文档以获取完整的自定义选项:



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

  delete.backgroundColor = [UIColor redColor]; 
more.backgroundColor = [UIColor colorWithRed:0.188绿色:0.514蓝色:0.984 alpha:1]; //任意颜色

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

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

UITableViewRowAction * delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@Deletehandler:^(UITableViewRowAction * action,NSIndexPath * indexPath)
{
//在这里删除一些东西
}];
delete.backgroundColor = [UIColor redColor];

UITableViewRowAction * more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@Morehandler:^(UITableViewRowAction * action,NSIndexPath * indexPath)
{
//就像一个例子:
[self presentUIAlertControllerActionSheet];
}];
more.backgroundColor = [UIColor colorWithRed:0.188绿色:0.514蓝色:0.984 alpha:1];

返回@ [删除,更多]; //包含所需按钮的数组。 1,2,3等...
}






如需进一步参考:链接到文档






为SWIFT SYNTAX编辑

 覆盖func tableView(tableView:UITableView,editActionsForRowAtIndexPath indexPath:NSIndexPath) - > [AnyObject]? {

var delete = UITableViewRowAction(style:UITableViewRowActionStyle.Default,title:Delete,handler:{(action:UITableViewRowAction!,indexPath:NSIndexPath!) - > Void in
/ /做一些事情
})
delete.backgroundColor = UIColor.redColor()

var more = UITableViewRowAction(style:UITableViewRowActionStyle.Default,title:More,handler:{ (action:UITableViewRowAction!,indexPath:NSIndexPath!) - > Void in
//做点什么
})
more.backgroundColor = UIColor.blueColor()

返回[删除,更多]
}


Currently I am using NSFetchedResultsController to handle tableviews. I am wondering is there any way to add additional buttons like the delete button in swipe operation?

I am thinking about to subclass it. But find nothing relevant in help docs to my problems.

Thanks in advance.

解决方案

Your title is misleading. NSFetchedResultsController has no relevance to your end goal. Subclassing would just create more work than necessary, what you want to look into is UITableViewRowAction. This handles very easily & similarly to the new UIAlertController, so you should feel extremely comfortable with this if you've already dabbled with the UIAlertController

A brief synopsis (straight from the docs) of UITableViewRowAction:

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.

A couple notes before you begin :

  • Apple implemented this in iOS8 : so think carefully about your deployment target. If you are coding strictly for i0S8 this is a quick and easy alternative to 3rd party libraries or creating a custom UITableView editing style. If you aim to retain compatibility for iOS7 your options are limited with this format. SIDE NOTE this does not cause a fatal error in your code, iOS7 apps will not crash, however, the custom actions simply won't display.
  • There is not much customizing beyond the title
  • The pros of this, is that everything is handled with blocks

Regardless, to implement a custom UITableViewRowAction you must first ensure your table is editable by calling the following methods:

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

At minimum, those are the two methods that need to be declared before moving on.

To actually customize your row action buttons, follow the general guideline as follows:

STEP 1 implement the tableView:editActionsForRowAtIndexPath: method

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

As you can see, it's an instance method of type NSArray, so you have to return an array.

STEP 2 Add action objects to pass in the array. An example:

{

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

}    

You can alter a few properties for these row actions, but see the documents for complete customization options:

To customize the row action background colors, simply preform it like most other actions within buttons:

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

STEP 3 As mentioned before you have to return an array in the instance method so your complete code should resemble the following :

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


For further reference : LINK TO DOCUMENTATION


EDITED FOR SWIFT SYNTAX

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

这篇关于在UITableView滑动中添加其他按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆