UITableviewCell的花式操作
2016-05-12 14:56
465 查看
UITableviewCell花式操作之删除置顶和标记
滑动后侧边栏出现删除、置顶和标记功能
下面直接上代码。。。
对于创建全局数组和创建UITableview这个我就直接跳过了(数组最好是用可变数组)
直接上UITableview的代理
然后就添加具体的视线方法,部分方法可以直接在UITableViewRowAction带的block方法块里面视线,但是部分方法需要其它代理的配合
比如说置顶方法
下面配置一些必要的代理
我这是不是就直接把demo代码贴上去了,在贴几个效果图
至于标记这个选项,原理就是对数据进行操作,具体怎么操作,根据自己的需求自己考虑,方法就在那里。。。。阿达!!!
效果展示
滑动后侧边栏出现删除、置顶和标记功能
下面直接上代码。。。
对于创建全局数组和创建UITableview这个我就直接跳过了(数组最好是用可变数组)
- (void)viewDidLoad { [super viewDidLoad]; cellArray = [NSMutableArray array]; for (int i = 0; i < 20; i ++) { [cellArray addObject:[NSString stringWithFormat:@"%d",i]]; } screeenWidth = [UIScreen mainScreen].bounds.size.width; screenHeight = [UIScreen mainScreen].bounds.size.height; [self makeTableView]; // Do any additional setup after loading the view, typically from a nib. } - (void)makeTableView{ _myTableView = [[UITableView alloc]initWithFrame:self.view.frame]; _myTableView.delegate = self; _myTableView.dataSource = self; [self.view addSubview:_myTableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return cellArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indetifier = @"celllllllll"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier]; } cell.textLabel.text = cellArray[indexPath.row]; return cell; }
直接上UITableview的代理
//这个是直接添加侧滑栏选项的代理,只支持在8.0之后使用 - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{ // 删除选项 UITableViewRowAction *deleAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [cellArray removeObject:[cellArray objectAtIndex:indexPath.row]]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }]; deleAction.backgroundColor = [UIColor redColor]; // 置顶选项 UITableViewRowAction *firstR = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self tableView:tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; }]; firstR.backgroundColor = [UIColor lightGrayColor]; // 标记选项,活着说是特殊操作选项。。。一般是针对数据进行处理 UITableViewRowAction *markerCell = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSString *biaoshiStr = [cellArray objectAtIndex:indexPath.row]; [cellArray removeObject:biaoshiStr]; biaoshiStr = [NSString stringWithFormat:@"%@+%@",biaoshiStr,biaoshiStr]; [cellArray insertObject:biaoshiStr atIndex:indexPath.row]; [tableView reloadData]; }]; markerCell.backgroundColor = [UIColor orangeColor]; return @[firstR,markerCell,deleAction]; }
然后就添加具体的视线方法,部分方法可以直接在UITableViewRowAction带的block方法块里面视线,但是部分方法需要其它代理的配合
比如说置顶方法
//置顶实现代码 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ NSString *fromStr = cellArray[sourceIndexPath.row]; [cellArray removeObject:fromStr]; [cellArray insertObject:fromStr atIndex:destinationIndexPath.row]; [tableView reloadData]; }
下面配置一些必要的代理
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
我这是不是就直接把demo代码贴上去了,在贴几个效果图
删除效果展示
标记效果展示
置顶效果展示
至于标记这个选项,原理就是对数据进行操作,具体怎么操作,根据自己的需求自己考虑,方法就在那里。。。。阿达!!!
相关文章推荐
- NGUI实现小地图,人物图标不动,地图移动
- NGUI实现小地图 地图不动,人物图标动
- 低版本net项目使用ueditor 图片上传配置
- Failed to read auto-increment value from the storage engine in MySQL
- 关于UITableView界面刷新
- JS&CSS压缩工具YUICompressor
- iOS开发之基础视图—— UIAlertView
- [Servlet编程]Request,Response使用与码表原理
- RGMII布线指导 RGMII Layout Guide
- UICollectionView瀑布流
- 报错: The type ByteInputStream is not accessible due to restriction on required library
- 网店UI设计排版
- 读书笔记(UIKit性能优化解析和Block经典实用)(一)
- Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- iOS 环信UI3.0集成中的问题
- 12RoundedTeamationBuilder
- iOS开发之基础视图—— UISlider
- 【Arduino官方教程第一辑】示例程序 4-3 数据图表
- quick 用系统浏览器打开url
- UIColor