iOS tableView多选,长按弹出菜单
2016-05-18 16:20
351 查看
1、tableView支持多选cell和单选cell,默认是单选的,如果要开启多选,需要定义tableView的一个属性,
2、其实tableView默认是支持cell的长按动作的,长按弹出菜单包括粘贴,复制,删除等等,但是必须实现tableView的3个方法,
不实现这3个方法的话,默认是不支持长按动作的。
3、很多产品的UITableViewcell支持左滑动,并且支持多种操作,扣扣的就支持置顶,删除等,
点击相应的操作,直接在block里就可以完成了。
// self.tableView.allowsMultipleSelection = YES; // 支持多选 // self.tableView.allowsMultipleSelectionDuringEditing = YES; // 编辑状态下支持多选
2、其实tableView默认是支持cell的长按动作的,长按弹出菜单包括粘贴,复制,删除等等,但是必须实现tableView的3个方法,
// 允许cell长按菜单 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0){ return YES; } // 允许可操作的Action (剪切,复制,粘贴,删除等等) - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0); { NSLog(@"%@",NSStringFromSelector(action)); return (action == @selector(copy:)); // 只支持copy return YES; // 支持所有 } // 操作Action - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0){ NSLog(@"%@",NSStringFromSelector(action)); if (action==@selector(copy:)) {//如果操作为复制 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];//黏贴板 [pasteBoard setString:cell.textLabel.text]; NSLog(@"%@",pasteBoard.string);//获得剪贴板的内容 } }
不实现这3个方法的话,默认是不支持长按动作的。
3、很多产品的UITableViewcell支持左滑动,并且支持多种操作,扣扣的就支持置顶,删除等,
// 要想编辑cell,必须支持此方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { NSLog(@"wfewfew");//获得剪贴板的内容 } } // 增加其他的操作,不返回nil的话,就会把系统的替换掉,UITableViewRowAction是iOS8之后才支持的 - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { // 执行相应的操作 }]; action1.backgroundColor = [UIColor grayColor]; UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { // 执行相应的操作 }]; action2.backgroundColor = [UIColor blueColor]; return @[action1,action2]; }
点击相应的操作,直接在block里就可以完成了。
相关文章推荐
- iOS 加载js获取webView中图片url
- iOS学习之——@Property
- iOS (static library)静态库的创建与使用
- iOS9 通讯录 CNContact
- iOS: ARC和非ARC下使用Block属性的问题
- Rollout学习1 简单使用篇
- iOS开发_统计xcode代码行数
- 我们究竟如何判断一个想法值不值得做呢?IOS开发
- ios缓存的问题处理
- iOS 证书
- iOS开发-----响应链
- iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)
- iOS学习之iOS沙盒(sandbox)机制和文件操作(二)
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
- IOS中的通知(NSNotifcation)
- iOS开发之自定义组件
- JS与iOS交互之OC获取JS内容跳转到指定控制器实现技巧
- 写个IOS应用程序,项目结构可以是这样......
- iOS js交互
- iOS的动态性