在UITableViewCell中显示UIMenuController,分组样式

Rui*_*pes 3 uitableview uimenucontroller

有没有一种简单的方法来实现复制菜单,而不是子类化UITableViewCell?

谢谢,

RL

ton*_*ung 10

在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 
Run Code Online (Sandbox Code Playgroud)

通过实现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];
    }
}
Run Code Online (Sandbox Code Playgroud)