iOS TableViewCell的展開與收縮

思路:通過刷新section來實現tableviewcell的展開與收縮
Demo:

#import "ViewController.h"
#define SCREENWIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *dataArray;//創建一個數據源數組
    NSMutableDictionary *dic;//創建一個字典進行判斷收縮還是展開
    NSArray *sectionArr;//分組的名字
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    dic = [NSMutableDictionary dictionary];
    dataArray = @[@[@"1"],@[@"1",@"2"],@[@"1",@"2",@"3"],@[@"1",@"2",@"3",@"4"]];
    sectionArr = @[@"第一組",@"第二組",@"第三組",@"第四組"];
    [self.view addSubview:self.tableView];
}

//懶加載
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
    view.backgroundColor = [UIColor blueColor];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    titleLab.text = sectionArr[section];
    titleLab.textColor = [UIColor blackColor];
    titleLab.userInteractionEnabled = true;
    [view addSubview:titleLab];
    //創建一個手勢進行點擊,這里可以換成button
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];
    view.tag = 300 + section;
    [view addGestureRecognizer:tap];
    return view;
}

- (void)action_tap:(UIGestureRecognizer *)tap{
    NSString *str = [NSString stringWithFormat:@"%ld",tap.view.tag - 300];
    if ([dic[str] integerValue] == 0) {//如果是0,就把1賦給字典,打開cell
        [dic setObject:@"1" forKey:str];
    }else{//反之關閉cell
        [dic setObject:@"0" forKey:str];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[str integerValue]]withRowAnimation:UITableViewRowAnimationFade];//有動畫的刷新
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([dic[string] integerValue] == 1 ) {  //打開cell返回數組的count
        NSArray *array = [NSArray arrayWithArray:dataArray[section]];
        return array.count;
    }else{
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor orangeColor];
    cell.textLabel.text = dataArray[indexPath.section][indexPath.row];
    return cell;
}

@end
測試.gif
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容