给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 ==@selector(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);
}

posted on 2016-06-15 11:53  X了个code  阅读(288)  评论(0编辑  收藏  举报