UITableViewController中的复制/粘贴功能 [英] Copy/Paste functionality in UITableViewController

查看:95
本文介绍了UITableViewController中的复制/粘贴功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewController。我想在用户触摸单元格时弹出复制/粘贴菜单。我想在联系人应用中执行此操作。如何实现此功能。有人可以帮助我。

I have a UITableViewController. I want to pop the copy/paste menu up when the user touches a cell. I want to do as in the Contacts app. How to implement this functionality. Can someone help me.

我试过这段代码,

UIMenuController *theMenu = [UIMenuController sharedMenuController];
[theMenu setTargetRect:CGRectMake(10, 200, 100, 40) inView:[self tableView]];
[theMenu setMenuVisible:YES animated:YES];

但它不起作用。我的问题是,

But it doesn't work. My question is,


  1. 我必须传递什么CGRect作为setTargetRect参数?

  2. 我是否需要在我的TableViewController中调用SetNeedsDisplayInRect吗?

  3. 还有什么可以使它工作?


推荐答案

如果我没有错误长按联系人中的单元格会出现复制/粘贴菜单?如果是这样,我将使用UILongPressGestureRecognizer类在单元格中长按。

If I am not wrong Copy/Paste menu appears when long pressing a cell in the contact right? If so, I will use UILongPressGestureRecognizer class to get the long press in the cell.

关于

1:传递单元格的矩形和inView:传递你的uitableView

1: pass the rect of the cell and inView: pass your uitableView

2:我觉得没必要

3:仅此而已:

这样的事情应该有效......

Something like this should work...

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MyCellIdentifier";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithReuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    cell.indentationWidth = cell.frame.size.height;

    UILongPressGestureRecognizer *r = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasLongPressed:)];
    [cell addGestureRecognizer:r];
    [r release];
    }

    //configure your cell here
    cell.textLabel.text = [file nameForCell]; 
    return cell;
}

- (void)cellWasLongPressed:(UILongPressGestureRecognizer *)recognizer{

    if (recognizer.state == UIGestureRecognizerStateRecognized) {
        //show your UIMenuHere
        UITableViewCell *cell = (UITableViewCell *)recognizer.view;
        UIMenuController *theMenu = [UIMenuController sharedMenuController];
        [theMenu setTargetRect:cell.frame inView:tableView];
        [theMenu setMenuVisible:YES animated:YES];
    }

}

注意:以上代码是脑编译的

Note: Above code is brain compiled

希望有所帮助;)

这篇关于UITableViewController中的复制/粘贴功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆