效果图:

简单说下实现思路:

数据传过来之后, 先创建好对应个数的分组头部View, 也就是要在

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

在这个方法返回的视图...我这里用的UIButton, 然后监听UIButton的点击, 然后重新刷新这一组

根据UIButton的selected状态来判断是否要展开, 如果是展开, 就返回该组对应的行数, 反之就返回0行就可以了

代码部分:

.h文件

#import <UIKit/UIKit.h>

@interface RPCategoryListController : UITableViewController

// 分组模型数据
@property (nonatomic, strong) NSArray *category; @end

.m文件

#import "RPCategoryListController.h"
#import "RPCategoryModel.h"
#import "RPChildCategoryModel.h" #define RPSectionTitleHeight 35 @interface RPCategoryListController () @property (nonatomic, strong) NSMutableArray *sectionTitleBtns; @end @implementation RPCategoryListController static NSString * const reuseIdentifier_category = @"Category"; #pragma mark - 懒加载 - (NSMutableArray *)sectionTitleBtns
{
if (!_sectionTitleBtns) {
_sectionTitleBtns = [[NSMutableArray alloc] init];
}
return _sectionTitleBtns;
} #pragma mark - 系统方法 - (instancetype)init
{
return [super initWithStyle:UITableViewStyleGrouped];
} - (void)viewDidLoad
{
[super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier_category];
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.category.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
UIButton *sectionTitleBtn; // 获取被点击的组标题按钮
for (UIButton *btn in self.sectionTitleBtns) {
if (btn.tag == section) {
sectionTitleBtn = btn;
break;
}
} // 判断是否展开
if (sectionTitleBtn.isSelected) {
RPCategoryModel *categoryModel = self.category[section];
return categoryModel.childs.count;
}
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RPChildCategoryModel *childCategoryModel = [self.category[indexPath.section] childs][indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_category forIndexPath:indexPath];
cell.textLabel.textColor = RPFontColor; if ([[RPInternationalControl userLanguage] isEqualToString:@"en"]) {
cell.textLabel.text = childCategoryModel.ename;
} else {
cell.textLabel.text = childCategoryModel.name;
}
return cell;
} - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.sectionTitleBtns[section];
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return RPSectionTitleHeight;
} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return ;
} #pragma mark - 私有方法 - (void)sectionTitleBtnClick:(UIButton *)sectionTitleBtn
{
// 修改组标题按钮的状态
sectionTitleBtn.selected = !sectionTitleBtn.isSelected; // 刷新单独一组
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionTitleBtn.tag];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
} - (void)setCategory:(NSArray *)category
{
_category = category; for (int index = ; index < category.count; index++) { // 组标题按钮的标题
NSString *title = self.category[index] name; // 创建组标题按钮
UIButton *sectionTitleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sectionTitleBtn.frame = CGRectMake(, , RP_SCREEN_WIDTH, RPSectionTitleHeight);
sectionTitleBtn.tag = index;
sectionTitleBtn.titleLabel.font = [UIFont systemFontOfSize:.f];
[sectionTitleBtn setTitle:title forState:UIControlStateNormal];
[sectionTitleBtn setTitleColor:RPFontColor forState:UIControlStateNormal];
[sectionTitleBtn setBackgroundColor:[UIColor whiteColor]];
[sectionTitleBtn addTarget:self action:@selector(sectionTitleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.sectionTitleBtns addObject:sectionTitleBtn]; // 组标题按钮底部分隔线
UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(, sectionTitleBtn.height - , sectionTitleBtn.width, )];
bottomLine.backgroundColor = RPNavBarColor;
bottomLine.alpha = 0.2;
[sectionTitleBtn addSubview:bottomLine];
}
}

关键代码:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionTitleBtn.tag];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

UITableView实现分组, 并且点击每个分组后展开的更多相关文章

  1. 微信分组群发45028,微信分组群发has no masssend quota hint

    微信分组群发45028,微信分组群发has no masssend quota hint >>>>>>>>>>>>>> ...

  2. Python中正则匹配使用findall,捕获分组(xxx)和非捕获分组(?:xxx)的差异

    转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例 ...

  3. Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能 先到官网(http://plugins.jquery.com/cookie/ )下载coo ...

  4. VS2010 使用时选择代码或双击时出错,点击窗口按钮后VS自动重启问题

    VS2010 使用时选择代码或双击时出错崩溃,点击窗口按钮后VS自动重启问题 下载补丁,打上补丁之后,重启电脑,解决了问题. WindowsXP的下载地址:Windows XP 更新程序 (KB971 ...

  5. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序     在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...

  6. MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出

    先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了. 首先 ...

  7. layui表格点击排序按钮后,表格绑定事件失效解决方法

    最近项目使用layui较为频繁,遇到了一个麻烦的问题,网上搜索也没有看到同类型的问题,故此记下来. 需求是点击上图右侧表格中某一个单元格,会触发点击事件如下代码: $("table>t ...

  8. 点击<a>标签后禁止页面跳至顶部

    一.点击<a>标签后禁止页面跳至顶部 1. 使用 href="javascript:void(0);",例如: <a href="javascript: ...

  9. php中点击下载按钮后待下载文件被清空

    在php中设置了文件下载,下载按钮使用表单的方式来提交 <form method="post" class="form-inline" role=&quo ...

随机推荐

  1. 浏览器的模式问题 Quirks Mode vs Standards Mode

    当微软开始产生与标准兼容的浏览器时,他们希望确保向后兼容性.为了实现这一点,他们IE6.0以后的版本在浏览器内嵌了两种表现模式: Standards Mode(标准模式或Strict Mode)和Qu ...

  2. Linux Makefile多目录的编写

    手头一个项目,需要编写项目的makefile 多目录结构: csource/ ├── common│   └── sqlite3├── inc│   ├── curl│   ├── lua│   └─ ...

  3. 『Python』 多线程 共享变量的实现

    简介: 对于Python2而言,对于一个全局变量,你的函数里如果只使用到了它的值,而没有对其赋值(指a = XXX这种写法)的话,就不需要声明global. 相反,如果你对其赋了值的话,那么你就需要声 ...

  4. flex中validateall()方法, 多Item验证 ,结果统一提示

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  5. x86 构架的 Arduino 开发板Intel Galileo

    RobotPeak是上海的一家硬件创业团队,团队致力于民用机器人平台系统.机器人操作系统(ROS)以及相关设备的设计研发,并尝试将日新月异的机器人技术融入人们的日常生活与娱乐当中.同时,RobotPe ...

  6. dnat,snat

    Iptables实现NAT是最基本的功能,大部分家用路由都是基于其SNAT方式上网,使用Iptables实现外网DNAT也很简单,不过经常会出现不能正常NAT的现象. 以下命令将客户端访问1.,很多人 ...

  7. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...

  8. 向Int数组插入随机1到100

    这是一个经典的面试题,考察了几个知识点 下边的代码是传统经典的做法 ]; ArrayList myList=new ArrayList(); Random rnd=new Random(); ) { ...

  9. php header()跳转

    test1.php <?PHP $g_user = "Jack"; echo $g_user; ?> test3.php <?PHP header('Locati ...

  10. convex optimization

    ##凸优化总结所有这些想法基本是来自于书籍[convex optimization](http://book.douban.com/subject/1888111/),主要包括凸优化的基本理论,主要的 ...