iOS:分组的表格视图UITableView,可以折叠和展开

简介:

  虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦。为了解决这个麻烦,可以将分组的行折叠和展开。折叠时,行内容就会隐藏起来;展开时,行内容就会显示出来。

折叠时:                        展开后:

     

  具体的代码如下:

复制代码
  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
  4 @property (weak, nonatomic) IBOutlet UITableView *tableView;
  5 @property (strong,nonatomic)NSArray *provinces;
  6 @property (strong,nonatomic)NSDictionary *cities;
  7 @property (strong,nonatomic)NSMutableArray *Cellstates;
  8 @end
  9 
 10 @implementation ViewController
 11 
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14     //初始化
 15     self.provinces = [NSArray array];
 16     self.cities = [[NSDictionary alloc]init];
 17     self.Cellstates = [NSMutableArray arrayWithCapacity:self.provinces.count];
 18     
 19     //加载数据
 20     NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
 21     NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
 22     
 23     if(dic)
 24     {
 25         //所有的省份
 26         self.provinces = [dic objectForKey:@"provinces"];
 27         
 28         //所有的城市
 29         self.cities = [dic objectForKey:@"cities"];
 30     }
 31     
 32     //默认每一个section都是折叠的
 33     for(int i=0; i<self.provinces.count; i++)
 34     {
 35         NSNumber *state = [NSNumber numberWithBool:NO];
 36         [self.Cellstates addObject:state];
 37     }
 38     
 39     //设置数据源和代理
 40     self.tableView.dataSource = self;
 41     self.tableView.delegate = self;
 42     
 43 }
 44 
 45 #pragma mark -tableView的数据源方法
 46 //有多少个分组
 47 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 48 {
 49     return self.provinces.count;
 50 }
 51 //每个分组有多少行
 52 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 53 {
 54     if([self.Cellstates[section] boolValue]) //展开的
 55     {
 56         //取出所有的城市
 57         NSArray *cities = [self.cities objectForKey:self.provinces[section]];
 58         return cities.count;
 59     }
 60     else //折叠的
 61     {
 62         return 0;
 63     }
 64 }
 65 //设置每一个单元格的内容
 66 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 67 {
 68     //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
 69     static NSString *reuseIdentifier = @"citiesCell";
 70     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
 71     //2.如果没有找到,自己创建单元格对象
 72     if(cell == nil)
 73     {
 74         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
 75     }
 76     //3.设置单元格对象的内容
 77     //取出所有的城市
 78     NSArray *cities = [self.cities objectForKey:self.provinces[indexPath.section]];
 79     cell.textLabel.text = cities[indexPath.row];
 80     //设置字体颜色
 81     cell.textLabel.textColor = [UIColor orangeColor];
 82     
 83     return cell;
 84 }
 85 //设置头部标题
 86 -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 87 {
 88     return self.provinces[section];
 89 }
 90 #pragma mark -tableView的代理方法
 91 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
 92 {
 93     UIButton *button = [[UIButton alloc]init];
 94     
 95     //设置标题
 96     [button setTitle:self.provinces[section] forState:UIControlStateNormal];
 97     
 98     //设置颜色
 99     [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
100     
101     //设置对齐方式
102     button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
103     
104     //设置字体大小
105     button.titleLabel.font = [UIFont systemFontOfSize:20];
106     
107     //添加事件
108     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
109     
110     //记住button的tag
111     button.tag = section;
112     
113     return button;
114 }
115 
116 #pragma mark -按钮的事件响应
117 -(void)buttonClicked:(UIButton*)sender
118 {
119     //1.取出旧状态
120     NSNumber *oldState = [self.Cellstates objectAtIndex:sender.tag];
121     
122     //2.创建新状态
123     NSNumber *newState = [NSNumber numberWithDouble:![oldState boolValue]];
124     
125     //3.删除旧状态
126     [self.Cellstates removeObjectAtIndex:sender.tag];
127     
128     //4.添加新状态
129     [self.Cellstates insertObject:newState atIndex:sender.tag];
130     
131     //刷新表格
132     [self.tableView reloadData];
133 }
134 @end
复制代码

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4793671.html,如需转载请自行联系原作者
目录
相关文章
|
10月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
170 0
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
202 0
|
缓存 算法 测试技术
iOS UITableView性能优化
iOS UITableView性能优化
iOS UITableView性能优化
|
iOS开发
ios贝塞尔曲线表格视图
x轴和y轴都是可自定义,曲线的值也是对应的值
106 0
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
439 0
|
iOS开发
iOS开发-关于UITableView去掉粘性的问题
iOS开发-关于UITableView去掉粘性的问题
122 0
|
iOS开发
iOS开发-加在透明视图上的控件会透明
iOS开发-加在透明视图上的控件会透明
162 0
|
iOS开发 开发者
iOS开发-简述UITableView中cell的重用问题
iOS开发-简述UITableView中cell的重用问题
210 0
|
前端开发 小程序 数据处理
iOS上传图片视图的封装:用法 【下篇】
iOS上传图片视图的封装:用法 【下篇】
264 0
iOS上传图片视图的封装:用法 【下篇】
|
iOS开发
iOS 水平方向弹出菜单(支持展开折叠)
iOS 水平方向弹出菜单(支持展开折叠)
359 0
iOS 水平方向弹出菜单(支持展开折叠)

热门文章

最新文章

  • 1
    苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
    48
  • 2
    苹果app上架app store 之苹果开发者账户在mac电脑上如何使用钥匙串访问-发行-APP发布证书ios_distribution.cer-优雅草卓伊凡
    44
  • 3
    uniapp云打包ios应用证书的获取方法,生成指南
    43
  • 4
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
    124
  • 5
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    29
  • 6
    iOS各个证书生成细节
    42
  • 7
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    213
  • 8
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    66
  • 9
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    85
  • 10
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    67