//
// TableViewController.m
// UITableView cell下拉
//
// Created by lanou3g on 15/8/10.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "TableViewController.h"
@interface TableViewController ()
@property(nonatomic,strong) UIButton *button ; //模拟cell的button
@property(nonatomic,strong) NSArray *sectionArray;//section标题
@property(nonatomic,strong) NSArray *rowArray;//模拟数据源
@property(nonatomic,strong) NSArray *rowArray2;//模拟数据源
@property(nonatomic,strong) NSMutableDictionary* dic;//用来判断分组展开与收缩的
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
//使taleView 没有下拉线
self.tableView.showsVerticalScrollIndicator = NO;
_sectionArray = @[@"体育",@"音乐",@"影视",@"生活",@"美食"];
_rowArray = @[@"体育",@"音乐",@"影视",@"生活",@"美食"];
_rowArray2 = @[@"七里香",@"稻花香",@"女儿红"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_sectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section %2 == 0) {
return [_rowArray count];
}else{
return [_rowArray2 count];
}
}
//展示表头
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//将button作为表头
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.tag = section;
[self.button setTitle:[NSString stringWithFormat:@"%@",_sectionArray[section]] forState:UIControlStateNormal];
self.button.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.7];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
return self.button;
}
//表头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_dic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
return 44;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (indexPath.section %2 == 0) {
cell.textLabel.text = [NSString stringWithFormat:@"第%ld个cell : %@",indexPath.row+1,_rowArray[indexPath.row]];
}else{
cell.textLabel.text = [NSString stringWithFormat:@"第%ld个cell : %@",indexPath.row+1,_rowArray2[indexPath.row]];
}
//决定子视图的显示范围
cell.clipsToBounds = YES;
return cell;
}
//展开收缩cell
-(void)buttonAction:(UIButton*)sender{
NSInteger didSection = sender.tag;
if (!_dic) {
_dic = [[NSMutableDictionary alloc]init];
}
NSString *key = [NSString stringWithFormat:@"%ld",didSection];
if (![_dic objectForKey:key]) {
[_dic setObject:@"" forKey:key];
}else{
[_dic removeObjectForKey:key];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:UITableViewRowAnimationFade];
}
@end
效果如下图