系統(tǒng)提供了 UITableViewCell 長按復(fù)制的代理方法闷游,遵守實現(xiàn)即可瞻鹏。
效果圖1
需要遵守 UITableViewDelegate
協(xié)議朦佩,并設(shè)置代理對象.
1.設(shè)置哪一行顯示尖滚。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;// 設(shè)置哪里顯示喉刘。
}
如果指定 cell 才有該功能 通過indexPath
判斷下。返回 YES 表示可以顯示漆弄。NO表示不顯示睦裳。
2. 指定哪一行顯示哪些操作
// 設(shè)置只有 section = 0 的cell 才能顯示并且只有 copy 一個操作
// 根據(jù) action 和 indexPath 來決定哪行哪些操作可以顯示
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
NSLog(@"-------%@",NSStringFromSelector(action));
if(action == @selector(copy:) && indexPath.section == 0){
return YES;
}
return NO;
}
該代理方法會多次調(diào)用來確定哪些操作可以顯示, 我通過打印 action 發(fā)現(xiàn)了 cut:、copy:撼唾、paste:廉邑、select:、selectAll: ....
一些操作, 但是當(dāng)我全部返回YES時發(fā)現(xiàn)顯示的只有cut:倒谷、copy:蛛蒙、paste:
這三個,并且當(dāng)我設(shè)置這三個其中一個為NO時, select:
或者其他非該三個 action 時 顯示的就只有兩個了恨锚,通過這樣的測試我發(fā)現(xiàn) 可以顯示的操作只有 cut:宇驾、copy:、paste:
猴伶。如果是我的操作不對造成的這樣的效果课舍,歡迎在評論區(qū)提出,謝謝.
全部返回YES的效果
3他挎、執(zhí)行操作筝尾,設(shè)置操作對應(yīng)的內(nèi)容
// 長按選擇操作后調(diào)用
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
// 根據(jù) indexPath 和 action 來判定是不是對應(yīng)的操作。
if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = phoneStr;
}
// 實現(xiàn)操作執(zhí)行后應(yīng)該實現(xiàn)的功能办桨。
}