整理文檔,搜刮出一個iOS中Cell的Section展開和收起的示例代碼,稍微整理精簡一下做下分享。
首先,先上圖,讓大家看看效果
相信大家對于TableViewd數據的設置都熟悉,這方面就不多說的,重點的還是來看:
1.如何實現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當前的數據 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];}
當用戶點擊到某一個cell時候,需要判斷cell是否是展開狀態,如果張開或者收起就調用
讓cell的section能夠重新加載刷新;
2.如何添加cell的Section
2.1設置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *sectionDic = self.dataSource[indexPath.section]; NSArray *cellArray = sectionDic[@"sub"]; //cell當前的數據 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; }}
上面的代碼是設置section的高度,主要是以cell的isshow作為標記,讓section的能夠隨數據的改變而變動
3.如果要在一個cell上再加一個cell,實現cell內嵌cell,需要在哪里加?
答案:當然是在cell的HeaderSection或者FooterSection上加上cell,這樣就能實現cell內嵌cell。
好了,說了那么多,估計大家還是喜歡看demo,以下是demo的鏈接:https://github.com/xiaojin1123/SectionOpenAndClose.git
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答