UITableView实现分组, 并且点击每个分组后展开

效果图:

简单说下实现思路:

数据传过来之后, 先创建好对应个数的分组头部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 0;
}

- (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 5;
}

#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 = 0; index < category.count; index++) {

        // 组标题按钮的标题
        NSString *title = self.category[index] name;

        // 创建组标题按钮
        UIButton *sectionTitleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sectionTitleBtn.frame = CGRectMake(0, 0, RP_SCREEN_WIDTH, RPSectionTitleHeight);
        sectionTitleBtn.tag = index;
        sectionTitleBtn.titleLabel.font = [UIFont systemFontOfSize:13.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(0, sectionTitleBtn.height - 1, sectionTitleBtn.width, 1)];
        bottomLine.backgroundColor = RPNavBarColor;
        bottomLine.alpha = 0.2;
        [sectionTitleBtn addSubview:bottomLine];
    }
}

关键代码:

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

时间: 2024-08-07 08:31:24

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

MySQL group by 单字分组序和多字段分组

我这里创建了一个 goods 表,先看下里面的数据: mysql> select * from goods; +----+------+------+------------+-------------+------------+ | id | s_id | b_id | goods_name | goods_price | goods_desc | +----+------+------+------------+-------------+------------+ |  1 |    1 

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

转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例题: 如图所示: 正则a和正则b两个式子匹配出来的结果是不同的. 那 ?: 的作用就是把捕获分组转变为非捕获分组. 什么是捕获组和非捕获组呢? (qq|163|126) ---> 这样单独的括号就为捕获组 (?:qq|163|126) ---> 这样在原有分组里加上?: 就把捕获组转变为一个非捕获

在程序中点击home键后将程序通知显示到状态栏中

当程序处于后台运作的时候,Activity处于onStop状态,so只要在onStop方法中将程序运行状态显示在状态栏即可 //在状态栏显示程序通知    private void showNotification() {        // 创建一个NotificationManager的引用        NotificationManager notificationManager = (NotificationManager) this                .getSystem

cocos2d-x:解决点击拖动按钮后,所在的layer监听不到触摸事件的问题

点击拖动按钮后,想让所在的layer监听到屏幕的触摸事件,可以把该按钮拖动开始的时候设置setTouchEnabled为false;例: #include "ui/UIButton.h" bool myDemo::init() { // 设置点击事件 EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(

Jquery插件 防刷新倒计时 “点击获取验证码后60秒内禁止重新获取

Jquery插件实现"点击获取验证码后60秒内禁止重新获取(防刷新)" 效果图: 先到官网(http://plugins.jQuery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下: 1 <style type="text/css"> 2 * {margin: 0; 3 padding: 0; 4 font-family: "Microsoft Yahei"; 5 } 6 .captcha-box { 7 w

【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住

当点击完 finishbutton后,dbca 或者dbua hang住 来源于: DBCA/DBUA APPEARS TO HANG AFTER CLICKING FINISH BUTTON (文档 ID 727290.1) 适用于: Oracle Database Configuration Assistant - Version 10.2.0.1 to 11.2.0.1 [Release 10.2 to 11.2] Information in this document applies

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

android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序 在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity, 结果总是存在好几个同样的activity,就算要返回的activity正在前台,点击消息后也会重新打开一个一样的activity,返回好几次才能退出, 而不能像qq之类的点击通知栏消息回到之前存在的activity,如果存在就不再新建一个activity 说的有点绕,如果是遇到此类问题的肯定能懂,没遇到

点击单选button后的文字就可以选定相应单选button

比方我想点击单选button后的文字就选中对应的button: <input type="radio" name="sex" value="1" id="men" /><label >男 </label> <input type="radio" name="sex" value="0" id="women"

页面实现验证码功能,点击“注册”按钮后,无论是否完成注册,验证码都能够自动刷新

要求页面实现验证码功能,点击"注册"按钮后,无论是否完成注册,验证码都能够自动刷新 <script> function validteCode() { var codes = new Array(4);       // var colors = new Array("Red","Green","Gray","Blue","Maroon","Aqua",&