在 UITableViewCell 中显示 UIMenuController,分组样式

发布于 2024-11-26 23:38:10 字数 80 浏览 0 评论 0原文

是否有一种简单的方法可以在点击单元格时实现复制菜单,而不是子类化 UITableViewCell?

谢谢,

RL

Is there a simple way to implement the copy menu when a cell is tapped, instead of subclassing the UITableViewCell?

thanks,

RL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2024-12-03 23:38:10

在iOS 5中,一个简单的方法是实现UITableViewDelegate方法:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 

通过实现3个委托,它将在长按手势后为您调用UIMenuController。像这样的例子:

/**
 allow UIMenuController to display menu
 */
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/**
 allow only action copy
 */
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 
{
    return action == @selector(copy:);
}

/**
 if copy action selected, set as cell detail text
 */
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 
{
    if (action == @selector(copy:))
    {
        UITableViewCell* cell = [tableView cellForIndexPath:indexPath];
        [[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
    }
}

In iOS 5, a simple way is to implement the UITableViewDelegate Methods:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 

By implementing the 3 delegates, it will enable call UIMenuController for you after a long press gesture. An example like:

/**
 allow UIMenuController to display menu
 */
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/**
 allow only action copy
 */
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 
{
    return action == @selector(copy:);
}

/**
 if copy action selected, set as cell detail text
 */
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 
{
    if (action == @selector(copy:))
    {
        UITableViewCell* cell = [tableView cellForIndexPath:indexPath];
        [[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
    }
}
浪漫之都 2024-12-03 23:38:10

是的!
从内部调用 [[UIMenuController sharedMenuController] setMenuVisible:YESanimated:ani] (其中 ani 是一个 BOOL 确定控制器是否应该动画化) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ( UITableView 的委托方法)

编辑: 上的“复制”命令默认情况下,UIMenuController 不会复制 detailTextLabel.text 文本。不过,有一个解决方法。将以下代码添加到您的类中。

-(void)copy:(id)sender {
    [[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}


- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if(action == @selector(copy:)) {
        return YES;
    }
    else {
        return [super canPerformAction:action withSender:sender];
    }
}

Yes!
Call [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (where ani is a BOOL determining whether the controller should be animated) from within - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ( UITableView's delegate method)

Edit: The 'copy' command on the UIMenuController will not by default copy the detailTextLabel.text text. However, there is a workaround. Add the following code into your class.

-(void)copy:(id)sender {
    [[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}


- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if(action == @selector(copy:)) {
        return YES;
    }
    else {
        return [super canPerformAction:action withSender:sender];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文