3

UITableViewCell をサブクラス化する代わりに、セルがタップされたときにコピー メニューを実装する簡単な方法はありますか?

ありがとう、

RL

4

2 に答える 2

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 

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];
    }
}
于 2011-12-29T05:31:59.767 に答える
2

はい!
内部から呼び出します[[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani](コントローラーをアニメーション化する必要があるかどうかaniを決定する場所) ( UITableView のデリゲート メソッド)BOOL- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

編集: の「コピー」コマンドは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];
    }
}
于 2011-08-05T17:04:15.610 に答える