我想要当我长按UITableViewCell以显示自定义UIMenuItems时弹出的UIMenuController。
我在viewDidLoad中设置了自定义项目
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];然后我设置了所有正确的委托方法。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return (action == @selector(copy:) || action == @selector(test:));
}
- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
// do stuff
}
return YES;
}但它所做的一切,就是显示“复制”项,因为我只允许它和我的自定义项。但是,自定义项不会显示。
我意识到,我可以向单元格本身添加一个手势识别器,但这有点违背了UIMenuController共享实例的目的,不是吗?
发布于 2012-09-06 07:44:59
据我了解,主要有两个问题:
1)你期望tableView canPerformAction:支持自定义选择器,而文档说它只支持两个UIResponderStandardEditActions (复制和/或粘贴);
2)由于您通过初始化menuItems属性来添加自定义菜单选项,因此不需要part || action == @selector(test:)。对于此项目选择器,检查将是自动的。
您可以执行以下操作来显示自定义菜单项:
1)修复表视图委托方法
a)
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
[[UIMenuController sharedMenuController] update];b)
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return (action == @selector(copy:));
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
// required
}2)使用设置单元(子类化UITableViewCell)
-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
return (action == @selector(copy:) || action == @selector(test:));
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
/// this methods will be called for the cell menu items
-(void) test: (id) sender {
}
-(void) copy:(id)sender {
}
///////////////////////////////////////////////////////发布于 2015-07-11 22:59:50
为UITableViewCell实现复制和自定义操作的:
在应用程序中注册后,注册自定义操作:
struct Token { static var token: dispatch_once_t = 0 }
dispatch_once(&Token.token) {
let customMenuItem = UIMenuItem(title: "Custom", action: #selector(MyCell.customMenuItemTapped(_:))
UIMenuController.sharedMenuController().menuItems = [customMenuItem]
UIMenuController.sharedMenuController().update()
}在您的UITableViewCell子类中,实现自定义方法:
func customMenuItemTapped(sender: UIMenuController) {
// implement custom action here
}在您的UITableViewDelegate,中,实现以下方法:
override func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return action == #selector(NSObject.copy(_:)) || action == #selector(MyCell.customMenuItemTapped(_:))
}
override func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
switch action {
case #selector(NSObject.copy(_:)):
// implement copy here
default:
assertionFailure()
}
}备注:
对于how to implement copy.上的信息,此答案使用新的Swift 3 #selectors.
发布于 2021-01-26 00:51:08
仅允许复制节0的第0行的示例
已更新至Swift 5.2
func shouldAllowCopyOn(indexPath: IndexPath) -> Bool {
if indexPath.section == 0 && indexPath.row == 0 {
return true
}
return false
}
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return self.shouldAllowCopyOn(indexPath: indexPath)
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
if (action == #selector(UIResponderStandardEditActions.copy(_:))) {
return self.shouldAllowCopyOn(indexPath: indexPath)
}
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if (action == #selector(UIResponderStandardEditActions.copy(_:)) && self.shouldAllowCopyOn(indexPath: indexPath)) {
if let cell = self.tableView.cellForRow(at: indexPath) as? UITableViewCell {
self.copyAction(cell: cell)
}
}
}
@objc
private func copyAction(cell: UITableViewCell) {
UIPasteboard.general.string = cell.titleLabel.text
}https://stackoverflow.com/questions/12290828
复制相似问题