uitableviewcell 为空时不允许复制

don't allow copy when uitableviewcell is empty

我试图让用户仅在 tableview 单元格具有值时进行复制。我正在以这种方式实现复制和粘贴:

- (BOOL)tableView:(nonnull UITableView *)tableView shouldShowMenuForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    RightCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //check if the cell is the header of the tableview:
    if ([cell isKindOfClass:[RightCellHeader class]]) {
        return NO;
    }else {
        return YES;
    }

}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)) {
        return YES;
    }else {
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    if (action == @selector(copy:) && pasteboard != nil) {
        RightCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [pasteboard setString:cell.commentLabel.text];

    }

}

所以!!!我错过了什么只允许 而不是空单元格 上的复制菜单 谢谢 :)

注意

当我 select 复制之前没有 select 一个字时,应用程序崩溃了。 图片:http://i61.tinypic.com/110zkhv.png

只是不允许UITableView委托方法中:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //Firstly find cell's text value
   RightCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   NSString *strText = cell.commentLabel.text;
   //Now check if text has got any value
   if(strText.length>0)
      return YES;
   else 
      return NO;
}