показать UIMenuController в UITableViewCell, сгруппированный стиль

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

Благодарность,

RL


person Rui Lopes    schedule 26.07.2011    source источник


Ответы (2)


Да!
Вызов [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (где ani – это BOOL, определяющий, следует ли анимировать контроллер) из - (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];
    }
}
person joshim5    schedule 05.08.2011
comment
Если вы поместите его внутрь tableView:didSelectRowAtIndexPath, то UIMenuController появится, когда вы выберете строку обычным способом (я так понимаю, это то, что вы хотите) - person joshim5; 06.08.2011
comment
да и нет :) то, что я хочу, это получить меню с опцией COPY, чтобы получить текст detailTextLabel.text, например, приложение «Контакты». - person Rui Lopes; 06.08.2011
comment
А, это немного сложнее. Я обновлю свой ответ, чтобы уточнить. - person joshim5; 06.08.2011
comment
Можно ли использовать эти методы делегирования и отображать контроллер меню одним касанием в didSelectRowAtIndexPath? - person W Dyson; 27.06.2012

В 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];
    }
}
person tony.tc.leung    schedule 29.12.2011