Simple way to show the 'Copy' popup on UITableViewCells like the address book App

后端 未结 7 1395
渐次进展
渐次进展 2020-12-12 13:38

Is there a simple way for subclasses of UITableViewCell to show the \'Copy\' UIMenuController popup like in the Address book app (see screenshot), after the selection is hel

7条回答
  •  执念已碎
    2020-12-12 13:57

     #pragma mark - COPY/PASTE Cell Text via Menu
    
    - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
    - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
    {
        return (action == @selector(copy:));
    }
    
    - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
    {
        if (action == @selector(copy:))
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
            [pasteBoard setString:cell.textLabel.text];
        }
    }
    

提交回复
热议问题