UITableView (3):显示cell上的菜单

问题:想让用户使用APP时,只要通过一个手指放在APP中一个TableViewcell上,就能在他们原本可选的操作中使用复制/粘贴选项

方案:

  在TabView的委托对象上实现下面3个UITableViewDelegate协议方法:

  tableView:shouldShowMenuForRowAtIndexPath:

    返回值为BOOL类型,如果返回YES,iOS将为TableViewCell显示快捷菜单

  tableView:canPerformAction:forRowAtIndexPath:withSender:

    返回值为BOOL类型,

  tableView:performAction:forRowAtIndexPath:withSender:  

    在Cell的快捷菜单里一旦你允许显示特定动作,并且一旦用户从菜单中选中了这个动作时,这个方法会在TableView的委托对象中调用.

    此时,必须采取任何满足用户需求的行动.例如:如果用户选择的"Copy"菜单,你将需要有一个粘贴板放那些被选中的TableView单元格的内容.

例子:

  现在我们将实现前面提到的在UITableViewDelegate协议中定义的3个方法,把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台:

#pragma mark - cell上显示菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    //允许菜单在每一行上显示
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    //把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台
    NSLog(@"%@",NSStringFromSelector(action));
    //现在允许所有的action
    return YES;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

}

运行程序,长按一个cell后,会显示一些菜单选项.控制台窗口打印出了所有可用菜单动作:

 cut:

 copy:

 select:

 selectAll:

 paste:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] delete:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _promptForReplace:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _transliterateChinese:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _showTextStyleOptions:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _define:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _addShortcut:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilitySpeak:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilitySpeakLanguageSelection:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilityPauseSpeaking:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] makeTextWritingDirectionRightToLeft:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] makeTextWritingDirectionLeftToRight:

上面这写是所有允许你显示给用户的一些动作.例如,如果你仅仅想让你的用户有Copy选项,在

tableView:canPerformAction:forRowAtIndexPath:withSender: 方法中简单找出你想显示的菜单选项,对其返回YES 否则返回NO

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    //把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台
    NSLog(@"%@",NSStringFromSelector(action));
    //现在允许所有的action
    //return YES;
    if (action == @selector(copy:)) {
        //允许显示复制选项
        return YES;
    }
    //其他的都不允许
    return NO;
}

下一步要捕获用户实际上从快捷菜单中选定的项目.在这些信息的基础上我们能选择合适的动作.

例如,如果用户从快捷菜单中选择了Copy,我们就可以用UIPasteBoard把这些cell复制到粘贴板上,以便后面使用:

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action == @selector(copy:)) {
        UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:indexPath];
        //创建粘贴板
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        [pasteBoard setString:cell.textLabel.text];
    }
}
时间: 2024-08-05 03:00:15

UITableView (3):显示cell上的菜单的相关文章

UITableView去除空白cell上多余separator

具体的效果可以参考微信ios7版的UITableview 它最后一行cell的separator是顶到最左边的 首先设置tableFooterView [objc] view plaincopy _messageTableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 然后在willDisplayCell上增加如下代码 控制最后一行separatorInset位置 [objc] view plaincopy - 

关于UITableView选中效果以及自定义cell上的控件响应事件

tableView默认的点击效果是:点击cell:A,出现点击效果,点另一个cell:B的时候,A的点击效果才会消失. 1.对于tableView,比较常用的效果,是点击表格行,出现效果,点击完毕,效果消失 那么就要在代码里做一些设置.代码如下: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath

【UIKit】UITableView.09 自定义cell

UITableView.09 自定义cell : 注意:在创建一个故事版的时候,需要将控制器的class修改成对应的class否则效果实现不了[如图] 1.这段代码就是用来设置cell所对应的xib,类似于绑定  // 1.想要使用文件包里面的资源就要使用[NSBundle mainBundle] // 2.loadNibNamed的意思是加载一个xib文件,名字为BookCell cell=[[[NSBundle mainBundle]loadNibNamed:@"BookCell"

iOS--JSON解析后如何获取数据,并且展示到相应cell上

首先建立一个singleview工程,并在故事版中添加UITableView,连好数据源和代理. 在.h文件中添加数据源和代理方法,并且声明一个UITableView的变量,代码如下. ? 1 2 3 4 5 6 7 #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (weak, nonat

IOS开发系列--TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式),ios7tableview索引

在此之前,我们已经创建了一个通过简单的表视图应用程序并显示预定义的图像.在本教程中,我们将继续努力,使应用程序变得更好,: >不同的行显示不同的图像 - 上个教程,我们的所有行显示相同的缩略图.那么不同的食物显示不同的图片不是更好么? >自定义视图单元-我们将展示我们自己的视图来替代默认表单元格样式 显示不同缩略图 在我们更改代码之前,让我们回顾显示缩略图的代码. 最后,我们增加了一个行代码指示UITableView每一行显示"creme_brelee.jpg"这张图片.显

iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)

一. 目的: 实现UITableViewCell上按钮点击事件可以进行页面跳转. 二. 实现方法: 1. 用协议的方式的实现. 2. 需要自定义UITableViewCell. 三. 代码部分. cell.h中 #import <UIKit/UIKit.h> @protocol SevenProtocolDelegate <NSObject> - (void)sevenProrocolMethod:(UIViewController *)viewController and:(NS

IE8“开发人员工具”使用详解上(各级菜单详解)

来源: http://www.cnblogs.com/JustinYoung/archive/2009/03/24/kaifarenyuangongju.html IE8“开发人员工具”使用详解上(各级菜单详解) IE8正式版已经发布了.本篇文章不会非常扯蛋地去进行什么评测,然后给出什么“Chrome运行JavaScript能力是IE8的15倍”.什么“IE8页面渲染速度是Safari的2.456倍”.什么“IE8的抗强暴能力比FireFox高出1.235倍” 这样的操蛋的结论.我管谁比谁强多少

iOS 如何设置tableview列表的cell上的的设置删除键 、置顶按钮、 未读信息按钮

思路很重要! 主要是写下我实现的具体思想,效果:首先,将cell向左滑动的时候,就会出现 删除  置顶 等按钮,那么我们就可以设计在cell上加两层的控件,第一层放的便是你要添加的 删除  置顶 等按钮  ,把这些按钮布置在cell的右侧  然后在第二层就是放一个uiview 这个uiview大小和cell的大小一样  然后再到uiview上添加一些显示数据的控件.  问题: 那第一层的 删除和置顶等按钮不是被覆盖了吗?  对 要的就是这种效果,现在的解决思路就是在uiview 上添加一个 pa

objc_setAssociatedObject获取cell上button对应所在的行

#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIButton *btnTest; @end #import "ViewController.h" #import "TestCell.h" #import <objc/runtime.h> static void *btnIndexPathKe