给tableView的cell上加长按转发,复制等功能

Posted X了个code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给tableView的cell上加长按转发,复制等功能相关的知识,希望对你有一定的参考价值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DLJobLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"otherCell"];
    WorkLogDataModel * model = [self.dataArray objectAtIndex:indexPath.row];
    
    [cell setup:model];
    //给cell加上长按手势
    UILongPressGestureRecognizer * longPressGesture =  [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    [cell addGestureRecognizer:longPressGesture];
    [longPressGesture release];

    return cell;
}

#pragma mark---长按复制转发相关的一些实现方法
// 此方法必须实现,不然会长按之后不会显示出
- (BOOL) canPerformAction: (SEL) action withSender: (id) sender
{
    
    if(action == @selector(handleCopyAndSendRoomCell:)||action [email protected](handleCopyCell:))
    {
        return YES;
    }
    return NO;
    
}
- (void)cellLongPress:(UIGestureRecognizer *)recognizer{
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        
        CGPoint location = [recognizer locationInView:self.tableView];
        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
        
        WorkLogDataModel * model = (WorkLogDataModel *)[self.dataArray objectAtIndex:indexPath.row];
        NSDate * timedate = [utils dateTimeToDate:model.createDate];
        NSString * time = [self titleFromDate:timedate];
        MemberModel *member = [UtilsForUM getMemberModelWithPhone:model.phone OrWithUmid:0];
        [UIPasteboard generalPasteboard].string = [NSString stringWithFormat:@"标题:%@\n内容:\n%@\n转自[%@]的工作日志",time,model.msg,member.name];
        
        if (indexPath !=nil) {
            DLJobLogTableViewCell *cell = (DLJobLogTableViewCell *)recognizer.view;
            //这里把cell做为第一响应(cell默认是无法成为responder,需要重写canBecomeFirstResponder方法)
            [cell becomeFirstResponder];
            UIMenuItem *itCopy = [[UIMenuItem alloc] initWithTitle:@"转发到群组" action:@selector(handleCopyAndSendRoomCell:)];
            
            UIMenuItem *itDelete = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(handleCopyCell:)];
            
            UIMenuController *menu = [UIMenuController sharedMenuController];
            
            [menu setMenuItems:[NSArray arrayWithObjects:itCopy,itDelete,nil]];
            [menu setTargetRect:cell.frame inView:cell.superview];
            [menu setMenuVisible:YES animated:YES];
            
            [itCopy release];
            [itDelete release];
            
        }
        
    }
    
}
//开启长按复制转发等的响应------此方法实现在自定义的cell中
- (BOOL)canBecomeFirstResponder{
    return YES;
}
-(void)handleCopyAndSendRoomCell:(id)sender
{
    D("%@",[UIPasteboard generalPasteboard].string);
    discussionGroupController * aDGC = [[discussionGroupController alloc]init];
    aDGC.jobLogString = [UIPasteboard generalPasteboard].string;
    [utils pushViewController:aDGC animated:YES];
    [aDGC release];
}
-(void)handleCopyCell:(id)sender
{
    D("%@",[UIPasteboard generalPasteboard].string);
}

以上是关于给tableView的cell上加长按转发,复制等功能的主要内容,如果未能解决你的问题,请参考以下文章

如何在 swift 4 中的 tableView Cell 中创建 collectionview? [复制]

给tableView设置headerView时遇到的问题

iOS 怎么给tableview空白处添加点击事件而不影响cell中的一些按钮的事件

iOS 在tableview的cell中的button上,添加选中状态的解答

iOS 如何设置tableview列表的cell上的的设置删除键 置顶按钮 未读信息按钮

iOS 关于tableview的性能优化问题