iOS二级Table的简单实现
2015-08-10 11:37
387 查看
最近在项目中遇到一个小问题,就是用户点击table中的一行,将展开该Cell展示一个subTable。整个功能类似于点击QQ好友的分组,展开显示该分组下的好友列表。 该事件发生在用户点击该Cell的时候,因此需要在下面方法中编写代码: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (((UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).isExpanded == NO) { ((UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).isExpanded = YES; NSMutableArray * indexPaths = [[NSMutableArray alloc] init]; for (int i = 1; i <= _subTable.count; i++) { NSIndexPath * subIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + i) inSection:indexPath.section]; [indexPaths addObject:subIndexPath]; } [_subDataArr addObjectsFromArray:_subTable]; [tableView beginUpdates]; [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation: UITableViewRowAnimationAutomatic]; [tableView endUpdates]; } else { ((UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).isExpanded = NO; NSMutableArray * indexPaths = [[NSMutableArray alloc] init]; for (int i = 1; i <= _subDataArr.count; i++) { NSIndexPath * subIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + i) inSection:indexPath.section]; [indexPaths addObject:subIndexPath]; } [_subDataArr removeAllObjects]; [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation: UITableViewRowAnimationAutomatic]; [tableView endUpdates]; } }
注释一下: isExpanded是UITableViewCell的属性,用来判断当前的Cell是否已经展了。这个属性是自己添加的。 _subTable是一个全局的数组,里面存储了二级列表中的数据。 _subDataArr是table的dataSource源,而且还控制着numberOfRowInSection这个方法的返回值。因此每次添加数据到这里面时,该section中的row也同样会发生变化。 该方法来添加二级列表比较简单,核心思想就是通过对NSIndexPath的操作来动态改变table的Cell数。使用了tableView的两个方法,一个add方法和一个delete方法。
相关文章推荐
- ios 判断空字符串
- ios:点击tablecell中的一个按钮,确定cell所在的行
- iOS 验证手机号
- ios百度地图的使用(普通定位、反地理编码)
- IOS 获取所有图片工具类
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
- ios常用的框架(源自知乎上的回答)
- iOS开发~优雅的使用Xcode帮助文档及帮助系统
- [IOS动画]KDProgressView(圆环)
- iOS定位 - 普通定位(没有地图) - 反地理编码(得到具体位置)
- IOS等待时动画效果的实现
- iOS 9应用开发教程之ios9的视图
- iOS6的旋屏控制技巧
- ios页面传值的几种方法
- iOS 9应用开发教程之ios9的视图
- iOS多线程编程之NSThread的使用
- 如何用代码控制以不同屏幕方向打开新页面【iOS】
- iOS平台XML解析类库对比和安装说名
- iOS开发——汉字转拼音
- iOS 定时器使用详解