先給大家看看效果圖吧
直接給大家看代碼吧,注釋都很詳細(xì)
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *sectionArray;//section標(biāo)題
@property(nonatomic, strong)NSMutableArray *rowInSectionArray;//section中的cell個(gè)數(shù)
@property(nonatomic, strong)NSMutableArray *selectedArray;//是否被點(diǎn)擊
@end
@implementation ViewController
-(void)loadView
{
[super loadView];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,20 , self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];
_tableView.tableFooterView = [[UIView alloc]init];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (void)viewDidLoad {
[super viewDidLoad];
_sectionArray = [NSMutableArray arrayWithObjects:@"標(biāo)題1",@"標(biāo)題2",@"標(biāo)題3",@"標(biāo)題4", nil];//每個(gè)分區(qū)的標(biāo)題
_rowInSectionArray = [NSMutableArray arrayWithObjects:@"4",@"2",@"5",@"6", nil];//每個(gè)分區(qū)中cell的個(gè)數(shù)
_selectedArray = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];//這個(gè)用于判斷展開(kāi)還是縮回當(dāng)前section的cell
}
#pragma mark cell的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _sectionArray[indexPath.section];
return cell;
}
#pragma mark cell的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//判斷section的標(biāo)記是否為1,如果是說(shuō)明為展開(kāi),就返回真實(shí)個(gè)數(shù),如果不是就說(shuō)明是縮回,返回0.
if ([_selectedArray[section] isEqualToString:@"1"]) {
return [_rowInSectionArray[section]integerValue];
}
return 0;
}
#pragma mark section的個(gè)數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _sectionArray.count;
}
#pragma cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
#pragma mark - section內(nèi)容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//每個(gè)section上面有一個(gè)button,給button一個(gè)tag值,用于在點(diǎn)擊事件中改變_selectedArray[button.tag - 1000]的值
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 40)];
sectionView.backgroundColor = [UIColor cyanColor];
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
sectionButton.frame = sectionView.frame;
[sectionButton setTitle:[_sectionArray objectAtIndex:section] forState:UIControlStateNormal];
[sectionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
sectionButton.tag = 1000 + section;
[sectionView addSubview:sectionButton];
return sectionView;
}
#pragma mark button點(diǎn)擊方法
-(void)buttonAction:(UIButton *)button
{
if ([_selectedArray[button.tag - 1000] isEqualToString:@"0"]) {
// for (NSInteger i = 0; i < _sectionArray.count; i++) {
// [_selectedArray replaceObjectAtIndex:i withObject:@"0"];
// [_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
// }
//如果當(dāng)前點(diǎn)擊的section是縮回的,那么點(diǎn)擊后就需要把它展開(kāi),是_selectedArray對(duì)應(yīng)的值為1,這樣當(dāng)前section返回cell的個(gè)數(shù)就變?yōu)檎鎸?shí)個(gè)數(shù),然后刷新這個(gè)section就行了
[_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"1"];
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
//如果當(dāng)前點(diǎn)擊的section是展開(kāi)的,那么點(diǎn)擊后就需要把它縮回,使_selectedArray對(duì)應(yīng)的值為0,這樣當(dāng)前section返回cell的個(gè)數(shù)變成0,然后刷新這個(gè)section就行了
[_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我在button的點(diǎn)擊方法里注釋了一段代碼,解除注釋后運(yùn)行的效果是這樣的
今天就到這里,祝大家開(kāi)心??