折疊式cell,點(diǎn)擊展開可以查看所有cell

/** 數(shù)據(jù)源? */

@property (nonatomic, strong) NSMutableArray *dataSource;

/** 記錄cell的是否折疊的狀態(tài) */

@property (nonatomic, strong) NSMutableArray *ledArr;


- (NSMutableArray *)dataSource

{

if (!_dataSource) {

_dataSource = [[NSMutableArray alloc]init];

for (int? i =0 ;i <= 10 ;i ++ ) {

[_dataSource addObject:@"dd"];

}

}

return _dataSource;

}

- (NSMutableArray *)ledArr

{

if (!_ledArr) {

_ledArr = [[NSMutableArray alloc]init];

for (int? i = 0 ; i <= 10 ; i ++) {

/**

*? 這里的YES是代表每個(gè)section的cell都需要折疊

*? NO的話是代表點(diǎn)擊了展開或者cell本身比較少無需折疊

*/

NSNumber *led = [NSNumber numberWithBool:YES];

[_ledArr addObject:led];

}

}

return _ledArr;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return self.ledArr.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

/**

*? 取出section對應(yīng)的狀態(tài)

*? YES代表需要折疊

*? NO代表顯示全部

*/

BOOL led = [self.ledArr[section] boolValue];

if (led)

{

return 4;

}

else

{

return self.dataSource.count;

}

}

/**

*? 這里使用兩種從xib加載的cell

*? 簡單演示了如何實(shí)現(xiàn)

*

*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.row == 0)

{

HeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadTableViewCell"];

if (!cell)

{

cell = [[[NSBundle mainBundle] loadNibNamed:@"HeadTableViewCell" owner:nil options:nil] firstObject];

}

return cell;

}

else

{

ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentTableViewCell"];

if (!cell)

{

cell = [[[NSBundle mainBundle] loadNibNamed:@"ContentTableViewCell" owner:nil options:nil] firstObject];

}

if(self.dataSource.count > 3)

{

if (indexPath.row == 3) {

BOOL led = [self.ledArr[indexPath.section] boolValue];

if (led)

{

cell.contentLabel.text = @"展開";

}else

{

cell.contentLabel.text = @"這里是內(nèi)容";

}

}

}

return cell;

}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

BOOL led = self.ledArr[indexPath.section];

if (led) {

if (indexPath.row == 3)

{

/**

*? 代表點(diǎn)擊了展開

*? 將之前的狀態(tài)移除

*? 添加為NO的狀態(tài)

*? 而后刷新表格

*/

NSNumber? *newLed = [NSNumber numberWithBool:NO];

[self.ledArr removeObjectAtIndex:indexPath.section];

[self.ledArr insertObject:newLed atIndex:indexPath.section];

[self.table reloadData];

}else

{

NSLog(@"section-%zd - row --%zd",indexPath.section,indexPath.row);

}

}

}

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

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

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