IOS开发(27)之UITableView的Cell显示长按快捷菜单
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显示长按快捷菜单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言笔记-22-Linux基础-信号
- 下一篇: 开源物联网平台