UITableView (3):显示cell上的菜单

问题:想让用户使用APP时,只要通过一个手指放在APP中一个TableViewcell上,就能在他们原本可选的操作中使用复制/粘贴选项

方案:

  在TabView的委托对象上实现下面3个UITableViewDelegate协议方法:

  tableView:shouldShowMenuForRowAtIndexPath: 

    返回值为BOOL类型,如果返回YES,iOS将为TableViewCell显示快捷菜单

  tableView:canPerformAction:forRowAtIndexPath:withSender:

    返回值为BOOL类型,

  tableView:performAction:forRowAtIndexPath:withSender:  

    在Cell的快捷菜单里一旦你允许显示特定动作,并且一旦用户从菜单中选中了这个动作时,这个方法会在TableView的委托对象中调用.

    此时,必须采取任何满足用户需求的行动.例如:如果用户选择的"Copy"菜单,你将需要有一个粘贴板放那些被选中的TableView单元格的内容.

例子:

  现在我们将实现前面提到的在UITableViewDelegate协议中定义的3个方法,把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台:

#pragma mark - cell上显示菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    //允许菜单在每一行上显示
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    //把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台
    NSLog(@"%@",NSStringFromSelector(action));
    //现在允许所有的action
    return YES;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
}

运行程序,长按一个cell后,会显示一些菜单选项.控制台窗口打印出了所有可用菜单动作:

 cut:

 copy:

 select:

 selectAll:

 paste:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] delete:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _promptForReplace:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _transliterateChinese:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _showTextStyleOptions:

2014-10-23 14:22:06.545 UITableView_Demo2[4891:686718] _define:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _addShortcut:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilitySpeak:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilitySpeakLanguageSelection:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] _accessibilityPauseSpeaking:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] makeTextWritingDirectionRightToLeft:

2014-10-23 14:22:06.546 UITableView_Demo2[4891:686718] makeTextWritingDirectionLeftToRight:

上面这写是所有允许你显示给用户的一些动作.例如,如果你仅仅想让你的用户有Copy选项,在

tableView:canPerformAction:forRowAtIndexPath:withSender: 方法中简单找出你想显示的菜单选项,对其返回YES 否则返回NO

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    //把可使用的动作(SEL类型)简单转化成字符串并且打印到控制台
    NSLog(@"%@",NSStringFromSelector(action));
    //现在允许所有的action
    //return YES;
    if (action == @selector(copy:)) {
        //允许显示复制选项
        return YES;
    }
    //其他的都不允许
    return NO;
}

下一步要捕获用户实际上从快捷菜单中选定的项目.在这些信息的基础上我们能选择合适的动作.

例如,如果用户从快捷菜单中选择了Copy,我们就可以用UIPasteBoard把这些cell复制到粘贴板上,以便后面使用:

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action == @selector(copy:)) {
        UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:indexPath];
        //创建粘贴板 
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        [pasteBoard setString:cell.textLabel.text];
    }
}
原文地址:https://www.cnblogs.com/safiri/p/4045790.html