1 前言

对于UITableView的Cell长按,可以触发快捷菜单,包括复制,粘贴之类的操作。

2 代码实例

ZYViewController.h

#import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理
@property(nonatomic,strong) UITableView *myTableView;
@end

ZYViewController.m

@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式
self.myTableView.delegate = self;//设置代理为自身
myTableView.dataSource = self;//设置数据源为自身
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
[self.view addSubview:myTableView];
}
//设置每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
//        result = 40.0f;
result = 80.0f;
}
return result;
}
//设置每个Section呈现多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
//每行像是的数据
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
}
//indexPath.section 表示section的索引 indexPath.row表示行数的索引
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
}
return result;
}
//点击某一行时候触发的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual:myTableView]) {
NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
}
}
//允许长按菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//允许每一个Action
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
NSLog(@"%@",NSStringFromSelector(action));
return YES;
}
//对一个给定的行告诉代表执行复制或粘贴操作内容,
-(BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action==@selector(copy:)) {//如果操作为复制
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];//黏贴板
[pasteBoard setString:cell.textLabel.text];
NSLog(@"%@",pasteBoard.string);//获得剪贴板的内容
return YES;
}
return NO;
}

运行结果:

控制台显示(包含按Copy按钮的操作):

2013-04-28 16:58:14.609 UITableViewTest1[1536:c07] _insertImage:

2013-04-28 16:58:14.613 UITableViewTest1[1536:c07] cut:

2013-04-28 16:58:14.615 UITableViewTest1[1536:c07] copy:

2013-04-28 16:58:14.616 UITableViewTest1[1536:c07] select:

2013-04-28 16:58:14.618 UITableViewTest1[1536:c07] selectAll:

2013-04-28 16:58:14.620 UITableViewTest1[1536:c07] paste:

2013-04-28 16:58:14.621 UITableViewTest1[1536:c07] delete:

2013-04-28 16:58:14.624 UITableViewTest1[1536:c07] _promptForReplace:

2013-04-28 16:58:14.626 UITableViewTest1[1536:c07] _showTextStyleOptions:

2013-04-28 16:58:14.628 UITableViewTest1[1536:c07] _define:

2013-04-28 16:58:14.629 UITableViewTest1[1536:c07] _addShortcut:

2013-04-28 16:58:14.631 UITableViewTest1[1536:c07] _accessibilitySpeak:

2013-04-28 16:58:14.633 UITableViewTest1[1536:c07] _accessibilitySpeakLanguageSelection:

2013-04-28 16:58:14.635 UITableViewTest1[1536:c07] _accessibilityPauseSpeaking:

2013-04-28 16:58:14.636 UITableViewTest1[1536:c07] makeTextWritingDirectionRightToLeft:

2013-04-28 16:58:14.638 UITableViewTest1[1536:c07] makeTextWritingDirectionLeftToRight:

2013-04-28 16:58:42.048 UITableViewTest1[1536:c07] copy:

2013-04-28 16:58:42.421 UITableViewTest1[1536:c07] Section 0,Cell 0

3 结语

以上就是所有内容,希望对大家有所帮助。

Demo实例下载:http://download.csdn.net/detail/u010013695/5312213

IOS开发(27)之UITableView的Cell显示长按快捷菜单相关推荐

  1. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇-UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h&g ...

  2. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇-UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

  3. ios开发——27个iOS开源库,让你的开发坐上火箭吧

    本文翻译自Medium,原作者是Paweł Białecki,原文 27个iOS开源库,让你的开发坐上火箭吧 你不会想错过他们,真的. 我爱开源. 并且我喜欢开发者们,把他们宝贵的私人时间用来创造神奇 ...

  4. IOS开发中实现UITableView按照首字母将集合进行检索分组

    在开发公司项目中遇到了将图书目录进行按照首字母分组排序的问题 1.在项目添加解析汉字拼音的Pinyin.h文件 /** pinyin.c*/ #define HANZI_START 19968 #de ...

  5. 【Android开发日常】一文弄懂桌面图标快捷菜单 桌面小组件

    本文将介绍如何创建和管理应用快捷方式.如何创建和管理应用桌面小组件. 目录 一.桌面菜单 1.1 概览 1.2.为什么需要桌面图标快捷菜单 1.3.如何实现桌面图标快捷菜单 1.3.1 创建静态快捷方 ...

  6. iOS开发中设置UITableView每组头试图与第一行cell之间的分割线

    UITableView中每组头试图与第一行cell之间默认有一行分割线,且分割线是顶格显示,如果想要设置这条分割线不顶格显示,方法如下: cell.selectionStyle = UITableVi ...

  7. iOS开发——27个开源库,让你的开发坐上火箭吧

    27个iOS开源库,让你的开发坐上火箭吧 你不会想错过他们,真的. 我爱开源. 并且我喜欢开发者们,把他们宝贵的私人时间用来创造神奇的东西,然后他们会和其他人分享并且不求回报.开源作者和贡献者,你们是 ...

  8. iOS开发Storyboard中UITableView顶部默认空白 - 芒果iOS

    [主要内容:] 1. 问题描述 2.问题分析 3. 解决问题办法 一.问题描述 前两天开发的时候在StoryBoard中创建了一个UITableView,但是拖到Controller里边之后,UITa ...

  9. iOS开发--底部按钮和应用图标显示未读消息

    我们要实现的效果如下: 我们使用系统自带的,实际上,代码量很少,在我们要显示的按钮上,打上下面一句代码即可: self.tabBarItem.badgeValue = @"1"; ...

最新文章

  1. 图像预处理第7步:标准归一化
  2. 使用 OpenMVG+PMVS实现视觉三维重建
  3. js 数据类型和转化
  4. Java Lambda表达式入门
  5. linux 检查 文件末尾 是否有空行
  6. python3 读取csv
  7. ASP.NET MVC Beta 新特性之 IValueProvider
  8. python里的join方法_python中join()方法介绍
  9. 洛阳师范学院计算机科学与技术专业怎么样,2019洛阳师范学院专业排名
  10. PSP(个体软件过程)
  11. 【语音识别】之梅尔频率倒谱系数(mfcc)及Python实现
  12. 求栈中元素个数算法_嵌入式必知基础算法(一)
  13. Ochestrator企业数据总线
  14. skl pipline 运行流程演示
  15. border(边框)的两种写法
  16. cad工具箱详细讲解_CAD工具箱的12种功能详解
  17. java isinterrupted_JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
  18. FL Studio教程之如何插入第三方插件
  19. 中国航信IBE机票代理人Shoppping查询指令
  20. 安全性测试:以用户登录为例

热门文章

  1. “一个好的药鼎对于炼药师来说,就如同武士手中的宝剑一般重要。”
  2. html 鼠标经过弹性flash导航代码,flash鼠标经过代码
  3. idea的version control 找不到 subversion
  4. IT行业实用的学习网站
  5. plt.xticks()的理解
  6. [cognexVisionPro]错误:Vpp_1.vpp包含Cognex.visionPro.ToolBlock.CogToolBloc而不是CogJob
  7. 得物AppH5秒开优化实战
  8. 新手如何快速入门深度学习
  9. mysql多select合并_mysql 多个字段合并
  10. WORD的三种选定文本的方法