效果图:

简单说下实现思路:

数据传过来之后, 先创建好对应个数的分组头部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. Android 数据库读取数据显示优化 Application [6]

    Application和Activity,Service一样是android框架的一个系统组件, 当android程序启动时系统会创建一个application对象,用来存储系统的一些信息. 通常我们 ...

  2. PHP之路——Mysql多表查询

    select a.id,a.`name` AS '姓名',b.`subject`,c.`achievement` from aaa AS a left join ccc AS c on a.id=c. ...

  3. 分布式配置管理平台 - Disconf介绍

    原博客地址:http://blog.csdn.net/zhu_tianwei/article/details/47984545 Disconf专注于各种分布式系统配置管理的通用组件/通用平台,提供统一 ...

  4. PIL Image 转成 wx.Image、wx.Bitmap

    import wx from PIL import Image def ConvertToWxImage(): pilImage = Image.open('1.png') image = wx.Em ...

  5. 线程通信机制:共享内存 VS 消息传递

    在并发编程中,我们必须考虑的问题时如何在两个线程间进行通讯.这里的通讯指的是不同的线程之间如何交换信息. 目前有两种方式: 1.共享内存 2.消息传递(actor 模型) 共享内存: 共享内存这种方式 ...

  6. POJ3041 Asteroids(二分图最大匹配)

    题目链接. 分析: 暂略. AC代码: #include <iostream> #include <cstdio> #include <cstring> #incl ...

  7. (转载)PHP删除数组中的特定元素的代码

    (转载)http://www.jb51.net/article/30689.htm 我们知道,PHP没有提供专门删除一个特定数组元素的方法.但是可以通过unset()函数来完成这种要求比如下面的程序: ...

  8. HelloSilverlight

    一:输入姓名并选中一个日期,将在下面显示 二:XAML代码 <UserControl x:Class="HelloSilverlight.MainPage" xmlns=&q ...

  9. yui--datatable基础和常用知识总结

    1.namespace 用于创建一个全局的命名空间,使用YUI时,首先会自动创建widget,util,example三个命名空间,使用时也可以自定义命名空间.类似于在程序中建了了一个static变量 ...

  10. 两个Select 之间的值的传递

    运行效果: 下面是代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML ...