2011-07-26 46 views

回答

2

是的!
呼叫[[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani](其中aniBOOL确定控制器是否应动画)从- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath(UITableView中的委托方法)

编辑中:在UIMenuController“复制”命令,默认不会复制detailTextLabel.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]; 
    } 
} 
+0

它应该完成一个长的水龙头,是否正确? –

+0

如果你把它放在'tableView:didSelectRowAtIndexPath'里面,那么当你以一种普通的方式选择这个行时,就会出现'UIMenuController'(我收集这就是你想要的) – joshim5

+0

yes and no :)想要的是通过COPY选项获取菜单,以获取detailTextLabel.text文本,如联系人应用程序 –

9

在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名代表,它将使长按手势之后打电话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]; 
    } 
} 
相关问题