UITableViewController中的复制/粘贴功能

Emp*_*ack 5 iphone

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

我试过这段代码,

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

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

  1. 我必须传递什么CGRect作为setTargetRect参数?
  2. 我需要在TableViewController中调用SetNeedsDisplayInRect吗?
  3. 还有什么可以做到这一点?

nac*_*o4d 7

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

关于

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

2:我觉得没必要

3:仅此而已:

像这样的东西应该工作......

- (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];
    }

}
Run Code Online (Sandbox Code Playgroud)

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

希望能帮助到你 ;)