在UIViewController里用UITableView及相关的tableHeaderView、tableFooterView实现三级连动页面页面展示效果

就拿一本书的章、节、知识点为例实现方案思路大体如下:

   将章、和节做为一级实现在UITableview里的每个section的header里边,原因是UITableView只提供了headerView的这种两级效果

在数据判断上将章、节、知识点做好区分,可以用type=1为章、type=2为节、type=3为知识点,这样在实现章、节、知识点的点击收缩时

效果及跳转不同的UIViewController时可以用type做好判断处理,同时在cell展示时可以控制好章、节、知识点UI展示,部分关键实现代码如下


一、将章和节的UIView组织的一个统一的UIView中并加到UIArray中作为章和节的UI展示在viewForHeaderInSection里边代码如下:

/** 
 * 创建分类视图
 */
- (void)loadModel
{
    int i = 0, sectionNum=1;
    
    //移除数据
    [_recommandSections removeAllObjects];
    [_sectionsAry  removeAllObjects];
    
    GroupCateView *cateView;
    NSArray *sectionAry;
    for (NSDictionary *section in _subjectListObject.chapterAry) {
        
        //获取节基本数据高度50
        sectionAry = [_subjectListObject.sectionDic objectForKey:[section objectForKey:@"id"]];
        if (![sectionAry count]) {
            continue;
        }
        
        [_sectionsAry addObject:section];
        
        //获取章基本数据 高度85
        cateView = [[GroupCateView alloc]  initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 50)];
        cateView.backgroundColor = [UIColor clearColor];
        [cateView hideViewWithSubjectSection];
        cateView.section    = 100000;
        cateView.userInteractionEnabled = NO;
        cateView.open       = NO;
        cateView.frame      = CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 85);
        cateView.type       = 0;
        
        //背景色
        UIView *_bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 85)];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.userInteractionEnabled = NO;
        
        //顶部灰色线
        UIView *topBgLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 19.5)];
        topBgLine.backgroundColor = UIBGCOLOR_245;
        [cateView addSubview:topBgLine];
        
        //分割线一
        UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 19.5, FD_SCREEN_MAIN_WIDTH, .5)];
        topLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        [_bgView addSubview:topLine];
        
        //第几章
        UILabel *_chapterNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 45, 15)];
        _chapterNumLabel.backgroundColor = [UIColor clearColor];
        _chapterNumLabel.font = [UIFont systemFontOfSize:14];
        _chapterNumLabel.text = [NSString stringWithFormat:@"第%d章", sectionNum++];
        _chapterNumLabel.textColor = [UIColor colorWithRed:58/255.0 green:135/255.0 blue:224/255.0 alpha:1.0];
        [_bgView addSubview:_chapterNumLabel];
        
        //章名称
        UILabel *_chapterNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 30, FD_SCREEN_MAIN_WIDTH-55-25, 15)];
        _chapterNameLabel.backgroundColor = [UIColor clearColor];
        _chapterNameLabel.text = [section objectForKey:@"name"];
        _chapterNameLabel.textColor = [UIColor blackColor];
        _chapterNameLabel.font = [UIFont systemFontOfSize:14];
        [_bgView addSubview:_chapterNameLabel];
        
        //章描述
        UILabel *_chapterIntroLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 48, FD_SCREEN_MAIN_WIDTH-30, 20)];
        _chapterIntroLabel.backgroundColor = [UIColor clearColor];
        _chapterIntroLabel.text = [NSString stringWithFormat:@"共%@小节,%@个核心考点", [section objectForKey:@"sections"], [section objectForKey:@"points"]];
        _chapterIntroLabel.font = [UIFont systemFontOfSize:13];
        _chapterIntroLabel.textColor = [UIColor grayColor];
        [_bgView addSubview:_chapterIntroLabel];
        
        //中部灰色线
        UIView *middleBgLine = [[UIView alloc] initWithFrame:CGRectMake(0, 84.5, FD_SCREEN_MAIN_WIDTH, .5)];
        middleBgLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        [_bgView addSubview:middleBgLine];
        
        cateView.backBtn.hidden = YES;
        cateView.backBtn.userInteractionEnabled = NO;
        cateView.cateName.hidden = YES;
        [cateView addSubview:_bgView];
        
        //底部灰色线
        UIView *bottomBgLine = [[UIView alloc] initWithFrame:CGRectMake(0, 85+49.5, FD_SCREEN_MAIN_WIDTH, .5)];
        bottomBgLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        [_bgView addSubview:bottomBgLine];
        
        cateView.backgroundColor = [UIColor whiteColor];
        [_recommandSections addObject:cateView];
        
        for (NSDictionary *sectionDic in sectionAry) {
            
            //获取节信息
            cateView = [[GroupCateView alloc]  initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 50)];
            cateView.backgroundColor = [UIColor clearColor];
            [cateView hideViewWithSubjectSection];
            cateView.delegate   = self;
            cateView.section    = i;
            cateView.open       = NO;
            //type:0章 type:1节
            cateView.type       = 1;
            
            //版块箭头左右图标
            UIImageView *_schoolArrayImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 19, 6, 12)];
            _schoolArrayImgView.tag  = 1000+i;
            _schoolArrayImgView.image = [UIImage imageNamed:@"icon_major_array_right"];
            [cateView addSubview:_schoolArrayImgView];
            
            //版块名称
            NSString *name = [NSString stringWithFormat:[sectionDic objectForKey:@"name"], i++];
            cateView.cateName.textColor         = [UIColor colorWithRed:(51.0/255.0f) green:(51.0/255.0f) blue:(51/255.0f) alpha:1.0f];
            cateView.cateName.textAlignment     = NSTextAlignmentLeft;
            cateView.cateName.font = [UIFont systemFontOfSize:14];
            cateView.cateName.backgroundColor   = [UIColor clearColor];
            cateView.cateName.contentMode       = UIViewContentModeCenter;
            cateView.cateName.text              = name;
            [cateView addSubview:cateView.cateName];
            
            cateView.frame = CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 50);
            cateView.flag       = 0;
            
            //分割线一
            cateView.backgroundColor = [UIColor clearColor];
            UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 49.5, FD_SCREEN_MAIN_WIDTH, .5)];
            topLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
            [cateView addSubview:topLine];
                
            //版块箭头左右图标
            _schoolArrayImgView.frame   = CGRectMake(10, 19, 6, 12);
            cateView.cateName.frame     = CGRectMake(23, 18, 260, 15);
            cateView.backBtn.frame      = CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, 50);
             cateView.backBtn.backgroundColor = [UIColor clearColor];
            
            cateView.backgroundColor = [UIColor whiteColor];
            [_recommandSections addObject:cateView];
            
            [_sectionsAry addObject:sectionDic];
            i++;
        }
    }
    
    //获取总段数
    _total = [_recommandSections count];
}

显示到section中的headerView视图里边

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GroupCateView *CateView = [_recommandSections objectAtIndex:section];
    if(CateView.type == 0){
        return 0;
    }
    
    //获取每个分类的CELL数据
    NSDictionary *sectionDic = [_sectionsAry  objectAtIndex:section];
    if ([[sectionDic objectForKey:@"type"] intValue] == 1) {
        return 0;
    }
    
    NSString *pid = [sectionDic objectForKey:@"id"];
    return CateView.open?[[_subjectListObject.pointDic objectForKey:pid] count]:0;
}

/** cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

/** 头部cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    float height;
    GroupCateView *cateView = [_recommandSections objectAtIndex:section];
    if (cateView.type ==  1) { //节
        height = 50;
    }else{ //章
        height = 85;
    }
    
    return height;
}

/**
 * section底部间距
 */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

/** 部cell视图 */
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    GroupCateView *cateView = [_recommandSections objectAtIndex:section];
    return cateView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _total;
}

heightForFooterInSection 的间距设置为0.01不设置的话用
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, FD_SCREEN_SHOW_HEIGHT)
                                              style:UITableViewStyleGrouped];
这咱方式展示时每个行会有默认间隔达不到UI上的效果

最后实现节与知识点的展示收缩效果即可,部分代码如下

#pragma mark - GroupCateViewdelegate
- (void)selectedWith:(GroupCateView *)view{
    
    UIImageView *_aImgView;
    
    if(view.tag > 5000) return;
    
    //将上一个小图标重置
    if (_tmpSection>=1000) {
        
        _aImgView = (UIImageView *)[self.view viewWithTag:(_tmpSection)];
        _aImgView.frame = CGRectMake(10, 19, 6, 12);
        _aImgView.image = [UIImage imageNamed:@"icon_major_array_right"];
    }
    
    //获取要更改的icon小图标
    _tmpSection = view.section+1000;
    _aImgView = (UIImageView *)[view viewWithTag:(1000+view.section)];
    
    if (view.open) {
        //控制icon图标方向
        _aImgView.frame = CGRectMake(10, 19, 6, 12);
        _aImgView.image = [UIImage imageNamed:@"icon_major_array_right"];
        
        for(int i = 0;i<[_recommandSections count];i++){
            
            GroupCateView *head = [_recommandSections objectAtIndex:i];
            head.open = NO;
            [head.backBtn setBackgroundImage:[UIImage imageNamed:@"btn_momal"] forState:UIControlStateNormal];
        }
        
        [_tableView reloadData];
        return;
    }else{
        
        //控制icon图标方向
        _aImgView.frame = CGRectMake(7, 23, 12, 6);
        _aImgView.image = [UIImage imageNamed:@"icon_major_array_down"];
    }
    
    _currentSection = view.section;
    [self reset];
}


/** 界面重置 */
- (void)reset
{
    for(int i = 0;i<[_recommandSections count];i++){
        GroupCateView *head = [_recommandSections objectAtIndex:i];
        
        if(head.section == _currentSection){
            head.open = YES;
            [head.backBtn setBackgroundImage:[UIImage imageNamed:@"btn_nomal"] forState:UIControlStateNormal];
            
        }else {
            [head.backBtn setBackgroundImage:[UIImage imageNamed:@"btn_momal"] forState:UIControlStateNormal];
            head.open = NO;
        }
    }
    
    [_tableView reloadData];
}

其中- (void)selectedWith:(GroupCateView *)view为GroupCateView代理这样点击每个节时就达了预期的效果:

实现效果如图:




  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
可以通过使用 UITableView实现一个日历表格。具体实现步骤如下: 1. 创建一个继承自 UIViewController 的类,例如名为 `CalendarViewController`。 2. 在 `CalendarViewController` 中创建一个 UITableView,并将其添加到当前视图中。 3. 实现 UITableViewDataSource 和 UITableViewDelegate 协议中的方法,以便为 UITableView 提供数据和响应用户的操作。 4. 在 UITableView 中创建 UITableViewCell,并为其添加子视图,例如 UILabel,来显示日期等信息。 5. 根据需要,可以使用 NSDate 和 NSCalendar 等类来计算日期和星期等信息。 6. 在 UIViewControllerviewDidLoad 方法中,设置 UITableView 的数据源和代理为当前的 `CalendarViewController` 对象。 下面是一个简单的示例代码,可以根据需要进行修改和扩展: ``` // CalendarViewController.h #import <UIKit/UIKit.h> @interface CalendarViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end // CalendarViewController.m #import "CalendarViewController.h" @interface CalendarViewController () @end @implementation CalendarViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建 UITableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回日历表格的总共行数 return 6; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回每一行日历表格的列数 return 7; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CalendarCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // 计算当前单元格对应的日期 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = indexPath.row + indexPath.section * 7; NSDate *date = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0]; // 显示日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"d"; cell.textLabel.text = [formatter stringFromDate:date]; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } @end ``` 在使用时,可以创建一个 `CalendarViewController` 对象,并将其添加到当前视图中即可: ``` CalendarViewController *calendarViewController = [[CalendarViewController alloc] init]; [self addChildViewController:calendarViewController]; [self.view addSubview:calendarViewController.tableView]; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值