zoukankan      html  css  js  c++  java
  • 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];

  • 相关阅读:
    一起谈.NET技术,深入ASP.NET 2.0的提供者模型(2) 狼人:
    一起谈.NET技术,从.NET中委托写法的演变谈开去(上):委托与匿名方法 狼人:
    一起谈.NET技术,将Flash 嵌入WPF 程序 狼人:
    一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析 狼人:
    人一生当中最应该珍惜的十种人
    《程序员的第一年》复习一下C#的【封装 多态 继承 简单计算器源码实例】
    myeclipse 修改模板
    三星将在百思买零售店内开设1400家体验店
    谷歌Q3推Android本 蚕食自己平板市场
    Spring Setting
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5245307.html
Copyright © 2011-2022 走看看