IOS开发(27)之UITableView的Cell显示长按快捷菜单

1 前言

对于UITableView的Cell长按,可以触发快捷菜单,包括复制,粘贴之类的操作。

2 代码实例

ZYViewController.h

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理

@property(nonatomic,strong) UITableView *myTableView;

@end

ZYViewController.m

@synthesize myTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式
    self.myTableView.delegate = self;//设置代理为自身
    myTableView.dataSource = self;//设置数据源为自身
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
    [self.view addSubview:myTableView];
    
}
//设置每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat result = 20.0f;
    if ([tableView isEqual:self.myTableView]) {
//        result = 40.0f;
        result = 80.0f;
    }
    return result;
}
//设置每个Section呈现多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
//每行像是的数据
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *result = nil;
    if ([tableView isEqual:myTableView]) {
        static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
        if (result == nil) {
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
        }
        //indexPath.section 表示section的索引 indexPath.row表示行数的索引
        result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
    }
    return result;
}
//点击某一行时候触发的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:myTableView]) {
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
    }
}
//允许长按菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//允许每一个Action
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    NSLog(@"%@",NSStringFromSelector(action));
    return YES;
}
//对一个给定的行告诉代表执行复制或粘贴操作内容,
-(BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action==@selector(copy:)) {//如果操作为复制
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];//黏贴板
        [pasteBoard setString:cell.textLabel.text];
        NSLog(@"%@",pasteBoard.string);//获得剪贴板的内容
        return YES;
    }
    return NO;
}

运行结果:


控制台显示(包含按Copy按钮的操作):

2013-04-28 16:58:14.609 UITableViewTest1[1536:c07] _insertImage:

2013-04-28 16:58:14.613 UITableViewTest1[1536:c07] cut:

2013-04-28 16:58:14.615 UITableViewTest1[1536:c07] copy:

2013-04-28 16:58:14.616 UITableViewTest1[1536:c07] select:

2013-04-28 16:58:14.618 UITableViewTest1[1536:c07] selectAll:

2013-04-28 16:58:14.620 UITableViewTest1[1536:c07] paste:

2013-04-28 16:58:14.621 UITableViewTest1[1536:c07] delete:

2013-04-28 16:58:14.624 UITableViewTest1[1536:c07] _promptForReplace:

2013-04-28 16:58:14.626 UITableViewTest1[1536:c07] _showTextStyleOptions:

2013-04-28 16:58:14.628 UITableViewTest1[1536:c07] _define:

2013-04-28 16:58:14.629 UITableViewTest1[1536:c07] _addShortcut:

2013-04-28 16:58:14.631 UITableViewTest1[1536:c07] _accessibilitySpeak:

2013-04-28 16:58:14.633 UITableViewTest1[1536:c07] _accessibilitySpeakLanguageSelection:

2013-04-28 16:58:14.635 UITableViewTest1[1536:c07] _accessibilityPauseSpeaking:

2013-04-28 16:58:14.636 UITableViewTest1[1536:c07] makeTextWritingDirectionRightToLeft:

2013-04-28 16:58:14.638 UITableViewTest1[1536:c07] makeTextWritingDirectionLeftToRight:

2013-04-28 16:58:42.048 UITableViewTest1[1536:c07] copy:

2013-04-28 16:58:42.421 UITableViewTest1[1536:c07] Section 0,Cell 0

3 结语

以上就是所有内容,希望对大家有所帮助。

Demo实例下载:http://download.csdn.net/detail/u010013695/5312213

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值