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只有一行展開的折疊效果,瀑布流,原生二維碼,還在持續更新中
    樊二哈閱讀 348評論 0 0
  • *7月8日上午 N:Block :跟一個函數塊差不多,會對里面所有的內容的引用計數+1,想要解決就用__block...
    炙冰閱讀 2,547評論 1 14
  • 1.OC里用到集合類是什么? 基本類型為:NSArray,NSSet以及NSDictionary 可變類型為:NS...
    輕皺眉頭淺憂思閱讀 1,394評論 0 3
  • 1.1 談一談GCD和NSOperation的區別? 首先二者都是多線程相關的概念,當然在使用中也是根據不同情境進...
    John_LS閱讀 1,340評論 0 12
  • 今天送孩子去培訓班,坐上了公交車。在車上,有個姑娘在問同車的乘客要去社保局怎么走,那位乘客熱情的跟她說怎么走。我忽...
    石頭大神閱讀 307評論 2 2