1.惯例 效果图奉上:
- 滚动列表的Section与Row分析(UICollectionView UITableView)
一个section 里面可以有多个row ,正是这个特性才能够达到分组效果
3.1 组头视图 对应的接口为:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
4 组的展开与收缩:
- 使用一个列表来记录当前组头的点击状态,点击的时候取反即可
//
// ViewController.m
// GroupTabView
//
// Created by 刘小兵 on 2018/6/8.
// Copyright © 2018 刘小兵. All rights reserved.
//
#import "ViewController.h"
#define kCell_Height 44
@interface ViewController (){
UITableView *expandTable;
NSMutableArray *dataSource;
NSMutableArray *sectionArray;
NSMutableArray *stateArray;
}
@end
@implementation ViewController
- (void)initDataSource
{
sectionArray = [NSMutableArray arrayWithObjects:@"东南大学",
@"南京大学",
@"上第交通大学",
@"武汉大学",nil];
NSArray *one = @[@"计算机",@"美术",@"音乐",@"经济管理"];
NSArray *two = @[@"物理",@"理论数学",@"金融",@"天体"];
NSArray *three = @[@"临床医学",@"计算机"];
NSArray *four = @[@"中文",@"计算机",@"汉语言文学",@"美术"];
dataSource = [NSMutableArray arrayWithObjects:one,two,three,four, nil];
stateArray = [NSMutableArray array];
for (int i = 0; i < dataSource.count; i++)
{
//所有的分区都是闭合
[stateArray addObject:@"0"];
}
}
- (void)viewDidLoad {
self.title = @"折叠的tableView";
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initDataSource];
[self initTable];
}
-(void)initTable{
expandTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
expandTable.dataSource = self;
expandTable.delegate = self;
expandTable.tableFooterView = [UIView new];
//[expandTable registerClass:[ExpandCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview:expandTable];
}
#pragma mark -
#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([stateArray[section] isEqualToString:@"1"]){
NSArray *array = [dataSource objectAtIndex:section];
return array.count;
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text= [NSString stringWithFormat:@"第%ld组的第%ld个cell",(long)indexPath.section,(long)indexPath.row];
//cell.clipsToBounds = YES;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return sectionArray[section];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[button setTag:section+1];
button.backgroundColor = [UIColor whiteColor];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
[line setImage:[UIImage imageNamed:@"line_real"]];
[button addSubview:line];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (kCell_Height-22)/2, 22, 22)];
[imgView setImage:[UIImage imageNamed:@"ico_faq_d"]];
[button addSubview:imgView];
UIImageView *_imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-30, (kCell_Height-6)/2, 10, 6)];
if ([stateArray[section] isEqualToString:@"0"]) {
_imgView.image = [UIImage imageNamed:@"ico_listdown"];
}else if ([stateArray[section] isEqualToString:@"1"]) {
_imgView.image = [UIImage imageNamed:@"ico_listup"];
}
[button addSubview:_imgView];
UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(45, (kCell_Height-20)/2, 200, 20)];
[tlabel setBackgroundColor:[UIColor clearColor]];
[tlabel setFont:[UIFont systemFontOfSize:14]];
[tlabel setText:sectionArray[section]];
[button addSubview:tlabel];
return button;
}
#pragma mark
#pragma mark -select cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)buttonPress:(UIButton *)sender//headButton点击
{
//判断状态值
if ([stateArray[sender.tag - 1] isEqualToString:@"1"]){
//修改
[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
}else{
[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
}
[expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kCell_Height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return kCell_Height;
}
@end