当uitableviewcell为空时不允许复制

时间:2015-08-27 15:28:28

标签: ios tableview copy-paste

我试图允许用户仅在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];

    }

}

因此!!!我错过了只允许非空单元格上的复制菜单 Tks:)

注意

当我选择复制而不先前选择一个单词时,应用程序崩溃了。 图片:http://i61.tinypic.com/110zkhv.png

1 个答案:

答案 0 :(得分:1)

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;
}