tableView 的cell功能操作方法
2016-07-15 16:17
465 查看
批量操作方法
// 允许在编辑模式进行多选操作 self.tableView.allowsMultipleSelectionDuringEditing = YES;
图1
- (IBAction)remove { // 获得所有被选中的行 NSArray *indexPaths = [self.tableView indexPathsForSelectedRows]; // 便利所有的行号 NSMutableArray *deletedDeals = [NSMutableArray array]; for (NSIndexPath *path in indexPaths) { [deletedDeals addObject:self.deals[path.row]]; } // 删除模型数据 [self.deals removeObjectsInArray:deletedDeals]; // 刷新表格 一定要刷新数据 [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop]; }
---------------------------
华丽的分割线------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 取消选中 [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
开启编辑
[self.tableView setEditing:!self.tableView.editing animated:YES]; // 有动画 /** * 返回indexPath对应的编辑样式 */ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView.isEditing) { // 如果是编辑模式,则返回插入操作 return UITableViewCellEditingStyleInsert; } return UITableViewCellEditingStyleDelete; } //哪几行可以编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row > 4) { return NO; } return YES; }
---------------------------
华丽的分割线------------------------
移动cell的方法
图2
//移动cell方法 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 如果原始位置与目标位置不相等 移动模型数组里的数据 if (sourceIndexPath != destinationIndexPath) { //取出原始位置移动的那一行的模型 id object = [self.contacts objectAtIndex:sourceIndexPath.row]; //删除 数组中原始索引对应的模型 object [self.contacts removeObjectAtIndex:sourceIndexPath.row]; if(destinationIndexPath.row > [self.contacts count]) { //如果目标位置大于数组的数量由于之前移除了一个模型 所以这里刚好模型数组的数量也减了1 //如果目标位置大于数组数 就把模型直接添加到最后一个位置 [self.contacts addObject:object]; } else { //如果目标位置小于数组数 就把模型数据插入到数组对应目标位置的地方 [self.contacts insertObject:object atIndex:destinationIndexPath.row]; } } //更新数据 [NSKeyedArchiver archiveRootObject:self.contacts toFile:FilePath]; }
//哪几行可以移动(可移动的行数小于等于可编辑的行数) - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row > 5) { return NO; } return YES; }
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnullaction, NSIndexPath * _NonnullindexPath) { NSLog(@"已删除"); }];w UITableViewRowAction *actionSign = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注微信" handler:^(UITableViewRowAction * _Nonnullaction, NSIndexPath * _NonnullindexPath) { NSLog(@"标记关注"); }]; actionSign.backgroundColor = [UIColor blueColor]; return @[action,actionSign]; }
全选按钮操作
- (void)allSelect:(UIButton*)sender{ NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[table indexPathsForVisibleRows]]; for (int i = 0; i < [anArrayOfIndexPath count]; i++) { NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i]; LTableViewCell *cell = (LTableViewCell*)[table cellForRowAtIndexPath:indexPath]; NSUInteger row = [indexPath row]; NSLog(@"%lu",(unsigned long)row); NSMutableDictionary *dic = [contacts objectAtIndex:row]; if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]) { [dic setObject:@"YES" forKey:@"checked"]; [cell setChecked:YES]; }else { [dic setObject:@"NO" forKey:@"checked"]; [cell setChecked:NO]; } } if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]){ for (NSDictionary *dic in contacts) { [dic setValue:@"YES" forKey:@"checked"]; } [(UIButton*)sender setTitle:@"取消" forState:UIControlStateNormal]; }else{ for (NSDictionary *dic in contacts) { [dic setValue:@"NO" forKey:@"checked"]; } [(UIButton*)sender setTitle:@"全选" forState:UIControlStateNormal]; } }
一旦当tabelView的cell被选中的时候 cell的子控件都会进入高亮状态,通过高亮状态可以设置cell子控件的图片和文字
// 设置高亮图片(cell选中 -> cell.imageView.highlighted = YES -> cell.imageView显示highlightedImage这个图片) cell.imageView.highlightedImage = [UIImage imageNamed:c.highlighted_icon]; // 设置label高亮时的文字颜色 cell.textLabel.highlightedTextColor = [UIColor redColor];
文/枫林晚_VG(简书作者)
原文链接:http://www.jianshu.com/p/54b9123ae42f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- 2205 Problem B
- 2204 Problem A(水)
- 中文转拼音 (utf8版,gbk转utf8也可用)
- 透过nginx实现内网hadoop、hbase集群对外访问web界面
- 最新cocoapods安装教程及问题解决
- CSS之选择器
- xml解析 StAX lterator读取
- 中缀表达式转换成后缀表达式并求值
- Spring + ehcache 缓存配置,注解实现
- python中 file.seek( )和file.readline() 的用法
- 如何对下载进行集中管理(低级a )
- linux编程基础
- magento layout xml 小结
- c 线程(平行世界)
- Android — 使用 sharedPreferences 本地保存 key-value 数据
- mycat 使用mysql实现全局序列号
- [置顶] Android Studio 之 发布APK
- 【JZOJ4628】立方体
- 向量的点乘与叉乘
- Hibrenante初探之单表映射