iphone - UITableViewController 中的复制/粘贴功能

标签 iphone

我有一个 UITableViewController。我想在用户触摸单元格时弹出复制/粘贴菜单。我想像在“通讯录”应用程序中一样进行操作。如何实现这个功能。有人可以帮助我吗?

我尝试了这段代码,

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

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

  1. 我必须将什么 CGRect 作为 setTargetRect 参数传递?
  2. 我需要在 TableViewController 中调用 SetNeedsDisplayInRect 吗?
  3. 还需要做什么才能使这项工作成功?

最佳答案

如果我没记错的话,长按联系人中的单元格时会出现复制/粘贴菜单,对吗?如果是这样,我将使用 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];
    }

}

注:以上代码为脑编译

希望有帮助;)

关于iphone - UITableViewController 中的复制/粘贴功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4582116/

相关文章:

iphone - 为什么我的应用程序崩溃了? UITable 和 Controller

iphone - 重置应用程序的推送通知设置

iphone - UIPageViewController + 自动布局旋转问题

iphone - 如何从 Xcode 项目中清理过时和排除的文件?

iphone - 不保存数据的外部类对象

iphone - 如何仅在 DEBUG 和 AdHoc 模式下执行特定功能

iphone - 处理文本字段中的非 ASCII 字符 :shouldChangeCharactersInRange:replacementString:

ios - UITableViewCell 中的 UIScrollView 在表格滚动时不断重新加载

ios - 标签栏图像未在 iOS 7.1 中显示

iphone - ScrollView 中的 UIScrollView - 未显示 ScrollBars