TableViewCell的Copy

本demo主要实现对表单元内容的复制(长按复制),为了方便用户对表视图的内容进行提取而设计。效果图如下:

  • 创建基于UITableViewCell类的CopyCell,.h,.m代码如下。

 

#import <UIKit/UIKit.h>
@class copyCell;

@protocol copyCellDelegate <NSObject>

@required
- (void)copyCell:(copyCell *)cell selectedCellAtIndexpath:(NSIndexPath *)indexpath;
- (void)copyCell:(copyCell *)cell deSelectedCellAtIndexpath:(NSIndexPath *)indexpath;
@optional
- (NSString *)copyCell:(copyCell *)cell dataForCellAtIndexpath:(NSIndexPath *)indexpath;

@end

@interface copyCell : UITableViewCell
{
    NSString *data;
    NSIndexPath *indexpath;
}
@property (nonatomic, strong) NSString *data;
@property (nonatomic, strong) NSIndexPath *indexpath;
@property (nonatomic, assign) id<copyCellDelegate> delegate;

- (void)initialize;
- (void)menuWillHide:(NSNotification *)notification;
- (void)menuWillShow:(NSNotification *)notification;
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer;


@end
#import "copyCell.h"
static const CFTimeInterval kLongPressMinimumDurationSeconds = 0.3;

@implementation copyCell
@synthesize data, indexpath, delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        return self;
    }
    [self initialize];
    return self;
}

- (void) initialize {
    self.data = nil;
    self.indexpath = nil;
    self.delegate = nil;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILongPressGestureRecognizer *longRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    [longRecognizer setMinimumPressDuration:kLongPressMinimumDurationSeconds];
    [self addGestureRecognizer:longRecognizer];
}

//设置选择和动画
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark Copy Menu related methods
//告知菜单栏哪些菜单可以显示
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:)) {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
//复制
- (void)copy:(id)sender {
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(copyCell:dataForCellAtIndexpath:)]) {
        NSString *dataText = [self.delegate copyCell:self dataForCellAtIndexpath:self.indexpath];
        [UIPasteboard generalPasteboard].string = dataText;
        NSLog(@"%@", dataText);
    }
    else if (self.data != nil) {
        [UIPasteboard generalPasteboard].string = self.data;
    }
    [self resignFirstResponder];
}
//判断对象是否为第一响应者,并返回Yes
- (BOOL) canBecomeFirstResponder {
    return YES;
}
//获取第一响应者
- (BOOL) becomeFirstResponder {
    return [super becomeFirstResponder];
}
//实现触摸
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self isFirstResponder] == NO) {
        return;
    }
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:YES animated:YES];
    [menu update];
    [self resignFirstResponder];
}

//隐藏菜单
- (void)menuWillHide:(NSNotification *)notification {
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(copyCell:deSelectedCellAtIndexpath:)]) {
        [self.delegate copyCell:self deSelectedCellAtIndexpath:self.indexpath];
    }
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil];
}

//显示菜单
- (void)menuWillShow:(NSNotification *)notification {
    self.selectionStyle = UITableViewCellSelectionStyleBlue;
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(copyCell:selectedCellAtIndexpath:)]) {
        [self.delegate copyCell:self selectedCellAtIndexpath:self.indexpath];
    }
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(menuWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil];
}

#pragma mark UILongPressGestureRecognizer Handler Methods
//长按
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
    if (longPressRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    if ([self becomeFirstResponder] == NO) {
        return;
    }
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.bounds inView:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];
    [menu setMenuVisible:YES animated:YES];
}
@end

 

  • viewController部分代码

        遵循协议,实现代理。代理方法:

        

- (void)copyCell:(copyCell *)cell selectedCellAtIndexpath:(NSIndexPath *)indexpath {
    [self.tableView selectRowAtIndexPath:indexpath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
- (void)copyCell:(copyCell *)cell deSelectedCellAtIndexpath:(NSIndexPath *)indexpath {
    [self.tableView deselectRowAtIndexPath:indexpath animated:NO];
    
}

- (NSString *)copyCell:(copyCell *)cell dataForCellAtIndexpath:(NSIndexPath *)indexpath {
    if (indexpath.row < _dataArray.count) {
        return [_dataArray objectAtIndex:indexpath.row];
    }
    
    return @"";
}

 

posted @ 2015-12-08 10:35  DevoutSoul  阅读(205)  评论(0)    收藏  举报