系統(tǒng)提供了 UITableViewCell 長(zhǎng)按復(fù)制的代理方法,遵守實(shí)現(xiàn)即可。

效果圖1
需要遵守 UITableViewDelegate 協(xié)議,并設(shè)置代理對(duì)象.
1.設(shè)置哪一行顯示。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;// 設(shè)置哪里顯示。
}
如果指定 cell 才有該功能 通過(guò)indexPath判斷下。返回 YES 表示可以顯示。NO表示不顯示。
2. 指定哪一行顯示哪些操作
// 設(shè)置只有 section = 0 的cell 才能顯示并且只有 copy 一個(gè)操作
// 根據(jù) action 和 indexPath 來(lái)決定哪行哪些操作可以顯示
- (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;
}
該代理方法會(huì)多次調(diào)用來(lái)確定哪些操作可以顯示, 我通過(guò)打印 action 發(fā)現(xiàn)了 cut:、copy:、paste:、select:、selectAll: ....一些操作, 但是當(dāng)我全部返回YES時(shí)發(fā)現(xiàn)顯示的只有cut:、copy:、paste: 這三個(gè),并且當(dāng)我設(shè)置這三個(gè)其中一個(gè)為NO時(shí), select:或者其他非該三個(gè) action 時(shí) 顯示的就只有兩個(gè)了,通過(guò)這樣的測(cè)試我發(fā)現(xiàn) 可以顯示的操作只有 cut:、copy:、paste:。如果是我的操作不對(duì)造成的這樣的效果,歡迎在評(píng)論區(qū)提出,謝謝.

全部返回YES的效果
3、執(zhí)行操作,設(shè)置操作對(duì)應(yīng)的內(nèi)容
// 長(zhǎng)按選擇操作后調(diào)用
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
// 根據(jù) indexPath 和 action 來(lái)判定是不是對(duì)應(yīng)的操作。
if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = phoneStr;
}
// 實(shí)現(xiàn)操作執(zhí)行后應(yīng)該實(shí)現(xiàn)的功能。
}