Skip to content

Instantly share code, notes, and snippets.

@ripperhe
Created September 18, 2018 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripperhe/861b63709fa25fbc615a18d3a4fae5ce to your computer and use it in GitHub Desktop.
Save ripperhe/861b63709fa25fbc615a18d3a4fae5ce to your computer and use it in GitHub Desktop.
UITableViewCell 实现长按调用 UIMenuController
// UITableView 所在的 window.level 必须为 0;其实是不是 keyWindow 也不是必须的,只要这个 window 显示在 level 为 0 的最上面
// UIMenuController 的 window 等级太低,所以要求目标 view 的 window 为 0
// 以下三个代理方法必须同时实现
// 这里仅展示了 Copy menu,如需其他 Menu 自行添加
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
// 如果只要 copy 方法,则这样写
if (action == @selector(copy:)) {
return YES;
}
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(copy:)) {
// iOS 模拟器和真机剪切板不互通的问题
// https://stackoverflow.com/questions/15188852/copy-and-paste-text-into-ios-simulator
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment