iOS 系統本身為可編輯的文字提供了複製、貼上、剪切這一整套快捷菜單。有趣的是,UITableViewCell 也能借用同一套機制:只要適度實作幾個代理方法,長按某一行就會彈出類似的操作菜單,讓使用者可以直接複製該行的內容。本文示範如何開啟這個能力,並把複製的結果寫入剪貼板。
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];
self.view.backgroundColor = [UIColor whiteColor];
// UITableViewStylePlain 為普通樣式,UITableViewStyleGrouped 為分組樣式
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.delegate = self;
myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
}
// 每行高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
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";
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:tableViewCellIdentifier];
}
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;
}
// 關鍵二:決定哪些操作可以被執行
-(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;
}
這三個方法缺一不可:shouldShowMenuForRowAtIndexPath: 決定菜單是否出現,canPerformAction:forRowAtIndexPath:withSender: 決定菜單裡列出哪些項目,performAction: 則負責實際行為。實務上通常不會像範例這樣全部 return YES,而是只放行 copy: 與 paste:,避免選單過長影響操作效率。
長按後菜單彈出:
控制台會先印出系統所有可回應的 action 名稱,按下 Copy 之後才會出現最後那筆 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: