实现折叠的uitableviewcell效果
2016-01-09 21:30
465 查看
实现折叠的uitableviewcell效果思路:
效果就像QQ联系人列表那种,分几组,点击可以展开里面的子列表。一开始想到的可以使用tableview group的这种形式来做,今天介绍的不是使用group而是使用Plain来实现的,重点就在于实现section这块,我把section+cell看做一个整体的cell这样来处理,效果图如下:
整个cell是由一个View+tableview来组成的,上面的view可以当然head section ,下面的tableview可以当作是点击展开后的二级列表cell,基于这个只要处理点击后的高度计算就可以了。
项目里实现第一个head 和最后一个head是展开状态,其它只要点击时在展开的思路是:创建一个NSMutableArray来保存每个head
section的状态,在
heightForRowAtIndexPath方法里根据状态来计算高度。
在viewdidload里记录初始状态,
定义一个数组:
_sectionStatus = [[NSMutableArrayalloc]
init];
for (NSInteger i = 0; i <self.m_arr.count; i++) {
if (i==0) {
[_sectionStatus addObject:@1];
else if (i==(self.m_arr.count-1)){
[_sectionStatus addObject:@1];
}else{
[_sectionStatus addObject:@0];
}
}
[self.m_tableView reloadData];
heightForRowAtIndexPath方法里如下:
点击head section方法如下:
点击展开后的cell方法,在当前VC里实现自定义cell里tableview的点击方法的一个代理就可以了,如下:
到此就完成了。
效果就像QQ联系人列表那种,分几组,点击可以展开里面的子列表。一开始想到的可以使用tableview group的这种形式来做,今天介绍的不是使用group而是使用Plain来实现的,重点就在于实现section这块,我把section+cell看做一个整体的cell这样来处理,效果图如下:
整个cell是由一个View+tableview来组成的,上面的view可以当然head section ,下面的tableview可以当作是点击展开后的二级列表cell,基于这个只要处理点击后的高度计算就可以了。
项目里实现第一个head 和最后一个head是展开状态,其它只要点击时在展开的思路是:创建一个NSMutableArray来保存每个head
section的状态,在
heightForRowAtIndexPath方法里根据状态来计算高度。
在viewdidload里记录初始状态,
定义一个数组:
_sectionStatus = [[NSMutableArrayalloc]
init];
for (NSInteger i = 0; i <self.m_arr.count; i++) {
if (i==0) {
[_sectionStatus addObject:@1];
else if (i==(self.m_arr.count-1)){
[_sectionStatus addObject:@1];
}else{
[_sectionStatus addObject:@0];
}
}
[self.m_tableView reloadData];
heightForRowAtIndexPath方法里如下:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([_sectionStatus[indexPath.row] isEqualToNumber:@1]) { ProblemBodyModel *cellData = self.m_arr[indexPath.row]; return cellData.m_Item2.count * 44 + 40; }else{ return 40; } }
点击head section方法如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //是否展开 BOOL status = [_sectionStatus[indexPath.row] boolValue]; NSNumber *num = status? @0 :@1; [_sectionStatus replaceObjectAtIndex:indexPath.row withObject:num]; [self.m_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; }
点击展开后的cell方法,在当前VC里实现自定义cell里tableview的点击方法的一个代理就可以了,如下:
#pragma mark -- 展开cell的点击方法 - (void)tableViewCellDidSelectWithUrlID:(NSString *)urlId andCellType:(NSString *)type; { }
到此就完成了。
相关文章推荐
- UE4 环境搭建(一)
- query.setFirstResult解析
- UITableView的优化方案
- ucgui 窗口管理及回调函数的应用
- UI基础 - 简易的加法计算器实现
- UI课程(Wechat)
- UI组件之TextView及其子类(一)
- iOS UI-微博案例(通过代码自定义Cell)
- Android出现CursorWindow: Window is full: requested allocation 1354087 bytes, free space 141032 bytes,
- duilib corner属性的贴图技巧——让图片自动贴到控件的的某一边或者一角并自适应控件的大小
- DirectUI的初步分析
- 游戏世界坐标转化为UGUI屏幕坐标
- 解决UITableView分割线距左边有距离的办法
- 关于APUE(Advanced Programming in the Unix Environment)第三版 代码在ubuntu中的编译运行
- 解决UITableView分割线距左边有距离的办法
- IOS UIView圆角,阴影,边框,渐增光泽
- COM组件设计与应用(二)——GUID和接口
- 设置UIButton的文字显示位置、字体的大小、字体的颜色、加粗(特别是字体颜色设置的那句代码。。挺有用)
- iOS - UIViewContentMode
- mui ajax 带header 提交