在UITableViewCell中显示UIMenuController,分组样式
当点击单元格时,是否有一种简单的方法来实现复制菜单,而不是将UITableViewCell子类化 谢谢 是的在UITableViewCell中显示UIMenuController,分组样式,uitableview,uimenucontroller,Uitableview,Uimenucontroller,当点击单元格时,是否有一种简单的方法来实现复制菜单,而不是将UITableViewCell子类化 谢谢 是的 调用[[UIMenuController sharedMenuController]setMenuVisible:YES animated:ani](其中ani是一个BOOL确定控制器是否应设置动画)从-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath(UITable
调用
[[UIMenuController sharedMenuController]setMenuVisible:YES animated:ani]
(其中ani
是一个BOOL
确定控制器是否应设置动画)从-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
(UITableView的委托方法)
编辑:默认情况下,UIMenuController
上的“复制”命令不会复制detailtexlabel.text
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];
}
}
在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个委托,它将在长按手势后为您启用call 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];
}
}
如果你把它放在
tableView:didSelectRowAtIndexPath
中,那么当你以普通方式选择行时,UIMenuController
就会出现(我想这是你想要的)是和否:)我想要的是获得菜单,带有复制选项,以获得detailtextlab.text,就像联系人AppAh一样,这有点复杂。我将更新我的答案以澄清。是否可以使用这些委托方法并在didSelectRowAtIndexPath中的一次点击中显示菜单控制器?