整理文檔,搜刮出一個(gè)ios/197892.html">ios/252451.html">iOS中Cell的Section展開和收起的示例代碼,稍微整理精簡一下做下分享。
首先,先上圖,讓大家看看效果

相信大家對(duì)于TableViewd數(shù)據(jù)的設(shè)置都熟悉,這方面就不多說的,重點(diǎn)的還是來看:
1.如何實(shí)現(xiàn)cell的Section的展開和收起的效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; currentRow = indexPath.row; NSDictionary *sectionDic = self.dataSource[indexPath.section]; NSArray *cellArray = sectionDic[@"sub"]; //cell當(dāng)前的數(shù)據(jù) NSDictionary *cellData = cellArray[indexPath.row]; NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]]; CellModel *chapterModel = [self.cellOpen valueForKey:key]; chapterModel.isShow = !chapterModel.isShow; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];}當(dāng)用戶點(diǎn)擊到某一個(gè)cell時(shí)候,需要判斷cell是否是展開狀態(tài),如果張開或者收起就調(diào)用
讓cell的section能夠重新加載刷新;
2.如何添加cell的Section
2.1設(shè)置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *sectionDic = self.dataSource[indexPath.section]; NSArray *cellArray = sectionDic[@"sub"]; //cell當(dāng)前的數(shù)據(jù) NSDictionary *cellData = cellArray[indexPath.row]; NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]]; CellModel *model = [self.cellOpen valueForKey:key]; if (model.isShow) { return (model.pois.count+1)*60; } else { return 60; }}上面的代碼是設(shè)置section的高度,主要是以cell的isshow作為標(biāo)記,讓section的能夠隨數(shù)據(jù)的改變而變動(dòng)
3.如果要在一個(gè)cell上再加一個(gè)cell,實(shí)現(xiàn)cell內(nèi)嵌cell,需要在哪里加?
答案:當(dāng)然是在cell的HeaderSection或者FooterSection上加上cell,這樣就能實(shí)現(xiàn)cell內(nèi)嵌cell。
好了,說了那么多,估計(jì)大家還是喜歡看demo,以下是demo的鏈接:https://github.com/xiaojin1123/SectionOpenAndClose.git
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注