показать UIMenuController в UITableViewCell, сгруппированный стиль - PullRequest
3 голосов
/ 27 июля 2011

Есть ли простой способ реализовать меню копирования при касании ячейки вместо подкласса UITableViewCell?

спасибо,

RL

Ответы [ 2 ]

10 голосов
/ 29 декабря 2011

В 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];
    }
}
2 голосов
/ 05 августа 2011

Да!
Вызов [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (где ani - это BOOL определение того, должен ли контроллер быть анимированным) из - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath (метод делегата UITableView)

Редактировать:команда copy 'на 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];
    }
}
...