今天做一個UITableView的分組輸出饭尝,并且還能點(diǎn)擊隱藏分組的效果七婴。
手打了一個demo
收齊狀態(tài)
打開狀態(tài)
//
// 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數(shù)組 用來控制組的開關(guān)
BOOL isOpen[30];
}
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//一開始關(guān)閉組
for (int i = 0; i<30; i++) {
isOpen[i] = YES;
}
[self loaddata];
[self createTableView];
}
//隨便建點(diǎn)什么數(shù)據(jù)的
- (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
//組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSArray *allKeys = [dic allKeys];
return allKeys.count;
}
//單元格個數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
/*
這里就是如何把組收起來的秘訣了
讓單元格個數(shù)為0!
*/
if (isOpen[section]) {
return 0 ;
}
NSArray *allKeys = [dic allKeys];
NSString *keys = allKeys[section];
NSArray *value = [dic objectForKey:keys];
return value.count;
}
//單元格內(nèi)容
- (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 {
//創(chuàng)建組頭
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];
//創(chuàng)建一個觸摸事件淘这,button等都可以袁梗,這里用的是手勢識別
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//只點(diǎn)一下
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