我有一个 UITableViewController。我想在用户触摸单元格时弹出复制/粘贴菜单。我想像在“通讯录”应用程序中一样进行操作。如何实现这个功能。有人可以帮助我吗?
我尝试了这段代码,
UIMenuController *theMenu = [UIMenuController sharedMenuController];
[theMenu setTargetRect:CGRectMake(10, 200, 100, 40) inView:[self tableView]];
[theMenu setMenuVisible:YES animated:YES];
但是这不起作用。我的问题是,
- 我必须将什么 CGRect 作为 setTargetRect 参数传递?
- 我需要在 TableViewController 中调用 SetNeedsDisplayInRect 吗?
- 还需要做什么才能使这项工作成功?
最佳答案
如果我没记错的话,长按联系人中的单元格时会出现复制/粘贴菜单,对吗?如果是这样,我将使用 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/