日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/24 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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显示长按快捷菜单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。