iOS開發(fā)之Cell折疊實現(xiàn)(一)

實現(xiàn)的思路有很多,基本上都是通過點擊Header,然后將隱藏的Cell展示出來,再點擊一次,就把Cell給隱藏了,效果圖以及代碼如下:

demo演示

#import "ViewController.h"
#define Screenwidth [UIScreen mainScreen].bounds.size.width
#define ScreennHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{

    //簡單的說,30代表的最多個setion的數(shù)字,數(shù)字可以比section大,不能小
    BOOL close[30];
    /**
     *  BOOL flag[3]; 意思就是創(chuàng)建了一個含有三個bool值的數(shù)組,用的時候其實很簡單,假如你的數(shù)組中的第二個數(shù)據(jù)取反了,這樣寫就行 flag[1] = !flog[1],這是c里面的數(shù)組寫法,比如int類型的數(shù)組 就是 int arr[3]= {1,2,3}; 動態(tài)分配就是malloc
     */
}

@property (nonatomic,strong) UITableView *WBTableView;
@property (nonatomic,strong) NSArray *Close;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //這個的目的是為了使得啟動app時,單元格是收縮的
    for (int i=0; i<30; i++) {
        close[i] = YES;
    }
    //創(chuàng)建
    _WBTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, Screenwidth, ScreennHeight) style:UITableViewStylePlain];
    _WBTableView.dataSource = self;
    _WBTableView.delegate = self;
    
    [self.view addSubview:self.WBTableView];
    
}

//數(shù)據(jù)源方法的實現(xiàn)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (close[section]) {
        return 0;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld組 第%ld行",indexPath.section,indexPath.row];
    
    return cell;
}

//組頭的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

//創(chuàng)建組頭視圖
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIControl *view = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, 50)];
    view.tag = 1000 + section;
    view.backgroundColor = [UIColor colorWithRed:0.849 green:0.195 blue:0.258 alpha:0.7];;
    [view addTarget:self action:@selector(sectionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 4, 70, 30)];
    label.textColor = [UIColor colorWithRed:1.000 green:0.985 blue:0.996 alpha:1.000];
    label.font = [UIFont systemFontOfSize:14];
    label.text = [NSString stringWithFormat:@"第%ld組",section];
    [view addSubview:label];
    
    
    return view;
    
}

/**
 *  cell收縮/展開 刷新
 *
 *  @param view <#view description#>
 */
-(void)sectionClick:(UIControl *)view{
    
    //獲取點擊的組
    NSInteger i = view.tag - 1000;
    //取反
    close[i] = !close[i];
    //刷新列表
    NSIndexSet * index = [NSIndexSet indexSetWithIndex:i];
    [_WBTableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
}

demo下載:https://github.com/OwenJoe/TableViewCellFold.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容