UITableView分组输出

今天做一个UITableView的分组输出,并且还能点击隐藏分组的效果。

手打了一个demo


收齐状态
打开状态
//
//  ViewController.m
//  Demo-tableViewSection
//
//  Created by mac on 16/7/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#define kScreenW    [UIScreen mainScreen].bounds.size.width
#define kScreenH    [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSDictionary *dic;
    //一个bool数组 用来控制组的开关
    BOOL isOpen[30];
    
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //一开始关闭组
    for (int i = 0; i<30; i++) {
        isOpen[i] = YES;
    }

    [self loaddata];
    [self createTableView];
    
}
//随便建点什么数据的
- (void)loaddata {
    
    dic = @{@"海贼王":@[@"路飞",@"索隆",@"山治",@"娜美",@"妮可罗宾",@"乔巴"],
                      @"火影忍者":@[@"鸣人",@"佐助",@"小樱",@"卡卡西"],
                      @"美食的俘虏":@[@"阿虏"]
                          };   
}
- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, kScreenW, kScreenH - 20) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
#pragma mark-dataSource
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSArray *allKeys = [dic allKeys];
    return allKeys.count;    
}
//单元格个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    /*
        这里就是如何把组收起来的秘诀了
        让单元格个数为0!
     */
    if (isOpen[section]) {
        return  0 ;
    }
    
    NSArray *allKeys = [dic allKeys];
    
    NSString *keys = allKeys[section];
    
    NSArray *value = [dic objectForKey:keys];
    
    return value.count;
    
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
    }
    
    NSArray *allKeys = [dic allKeys];
    
    NSString *keys = allKeys[indexPath.section];
    
    NSArray *value = [dic objectForKey:keys];
    
    cell.textLabel.text = value[indexPath.row];
    
    return cell;
}
#pragma mark -delegate
//组高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

//组头
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //创建组头
    UIControl *headerView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    headerView.tag = 1000 + section;
    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"hotMovieBottomImage"]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    
    NSArray *allKeys = [dic allKeys];
    
    NSString *keys = allKeys[section];
    
    label.text = keys;

    [label sizeToFit];
    label.textColor = [UIColor whiteColor];
    
    label.backgroundColor = [UIColor clearColor];
    
    [headerView addSubview:label];
    //创建一个触摸事件,button等都可以,这里用的是手势识别
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //只点一下
    tap.numberOfTapsRequired = 1;
    [headerView addGestureRecognizer:tap];
    
    return headerView;
}
#pragma mark -tapAction
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    NSInteger index = tap.view.tag - 1000;
    isOpen[index] = ! isOpen[index];
    //刷新特定组
    NSIndexSet * set = [NSIndexSet indexSetWithIndex:index];
    //刷新并添加动画
    [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationMiddle];   
}


@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,255评论 4 61
  • 左一消失了?不,不会的,应该是眼花了吧。张晓摇摇头,默默地朝公交站台走去。 第二天,早自习是早上七点过十分开始,一...
    咚咚锵咚锵咚咚锵阅读 347评论 0 1
  • 之前上班第一个月后就注册这个简书了,但是之前在电影院工作没有多稳定,现在想起是因为想做一个一日一记,在踩单车的时候...
    理想是不要加班阅读 265评论 0 0
  • 2006年12月14号,上证指数刷新了历史高点,这一天也是我入市的时点。有意思的是,我研究生导师是位”老江湖“的券...
    五花王阅读 13,697评论 0 10