给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上加长按转发,复制等功能的主要内容,如果未能解决你的问题,请参考以下文章
ios 实现在tableViewCell上面添加长按手势 删除该条cell以及列表后台数据等