美文网首页iOS学习笔记
UITableView分组输出

UITableView分组输出

作者: hunterzhu | 来源:发表于2016-07-28 20:58 被阅读64次

今天做一个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

相关文章

网友评论

    本文标题:UITableView分组输出

    本文链接:https://www.haomeiwen.com/subject/yegajttx.html