TableView 折叠效果

公司里要做个一个Tableview的折叠效果.大概想了一下,有三种实现.1.直接建立Tableview,使用UIView的animateWithDuration动画.动态改变cell的大小 2. 用SectionHeaderView 来做折叠下的cell.通过数组的方式控制tableCell的显示. 3.是在网上看到的一个例子 用的是model来形成一个数据结构,自定义cell.

1. animateWithDuration

这种方法其实不是很好. 有时候会出现空指针.很多问题.不推荐.

2.SectionHeader

SectionHeader伪装的Cell
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    [button setTag:section+1];
    button.backgroundColor = [UIColor lightGrayColor];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];


    
    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;
}

逻辑判断
//In the first
//Section开头的名字
    sectionArray  = [NSMutableArray arrayWithObjects:@"使用步骤",
                     @"正常人体温度",nil];
    
//状态逻辑
    stateArray = [NSMutableArray array];
    for (int i = 0; i < 2; i++)
    {
        //所有的分区都是闭合
        [stateArray addObject:@"0"];
    }

//headButton点击时,就修改状态值.
- (void)buttonPress:(UIButton *)sender
{
    //判断状态值
    if ([stateArray[sender.tag - 1] isEqualToString:@"1"]){
        //修改
        [stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
    }else{
        [stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
    }
//每次点击后,会重载整个TableView
    [helpView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

//根据stateArray的状态来决定是否显示Row.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([stateArray[section] isEqualToString:@"1"]){
        //如果是展开状态
//        NSArray *array = [dataSource objectAtIndex:section];
//        return array.count;
        return 1;
    }else{
        //如果是闭合,返回0
        return 0;
    }
    
}
在cellForRowAtIndexPath里设置内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
//自定义cell
        [tableView registerClass:[ThermometerHistoryCell class] forCellReuseIdentifier:@"cell"];
        ThermometerHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = @"使用步骤";
        return cell;
    }else if(indexPath.section == 1){
        static NSString *identifier = @"bell";
        UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

        cell = [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier];
        UIImage *img =[UIImage imageNamed:@"biaoge"];
        UIImageView *formView = [[UIImageView alloc]initWithFrame:CGRectMake(9, 60, ScreenWidth-18, img.size.height)];
        formView.image = img;
        [cell.contentView addSubview:formView];
        
        return cell;
        
    }
    return nil;

}


3.WSTableviewTree

WSTableviewTree 下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • github地址 里面有一些小demo:tableView只有一行展开的折叠效果,瀑布流,原生二维码,还在持续更新中
    樊二哈阅读 2,649评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,311评论 1 14
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 5,199评论 0 3
  • 1.1 谈一谈GCD和NSOperation的区别? 首先二者都是多线程相关的概念,当然在使用中也是根据不同情境进...
    John_LS阅读 5,107评论 0 12
  • 今天送孩子去培训班,坐上了公交车。在车上,有个姑娘在问同车的乘客要去社保局怎么走,那位乘客热情的跟她说怎么走。我忽...
    石头大神阅读 2,435评论 2 2