iOS 實現(xiàn)類似QQ分組樣式的兩種方式
思路
思路很簡單,對模型數(shù)據(jù)操作或則控制界面顯示
先看下json部分數(shù)據(jù)
"chapterDtoList": [{ "token": null, "id": 1295, "chapterName": "第一章", "parentId": 0, "chapterLevel": 0, "attachmentUrl": "", "description": null, "startDateTimestamp": null, "endDateTimestamp": null, "startDate": 1490889600000, "endDate": 1491062400000, "browseCount": 0, "workId": null, "chapterStatus": 3, "hadRead": 0, "subChapterList": [{ "token": null, "id": 1296, "chapterName": "第一節(jié)", "parentId": 1295, "chapterLevel": 1, "attachmentUrl": "", "description": null, "startDateTimestamp": null, "endDateTimestamp": null, "startDate": null, "endDate": null, "browseCount": 0, "workId": null, "chapterStatus": null, "hadRead": 0, "subChapterList": [], "classUserReadInfo": [] },
這種數(shù)據(jù)對應的一般都是個tableView, 然后根據(jù)章節(jié)分開,最終界面如下:
分析
這里采用UITableViewStylePlain樣式,chapterDtoList對應章,subChapterList對應節(jié)。章的話我們使用headerView來做,節(jié)的話我們使用cell來做。然后只需要給headerView添加一個點擊手勢,點擊的時候給對應的模型添加標識,從而去控制章節(jié)的收合。
方法一:
對模型數(shù)組進行操作,我們可以將返回的json數(shù)據(jù)轉(zhuǎn)化為兩個模型數(shù)組chapterListArray和tempChapterListArray,通過控制subChapterList的count來實現(xiàn)。界面的模型數(shù)據(jù)統(tǒng)一使用tempChapterListArray,展開與合并就等價于是否將“章數(shù)組“中的”節(jié)數(shù)組“賦值為nil
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.tempChapterListArray[section]; return onlineTaskDetailModel.subChapterList.count; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { YJTOnlineChapeterCell *headerView = [tableView dequeueReusableCellWithIdentifier:onlineChapeterCell]; YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.chapterListArray[section]; headerView.backgroundColor = [UIColor whiteColor]; headerView.onlineTaskDetailModel = onlineTaskDetailModel; if (section == 0) { headerView.tipsLableHeight.constant = 30; }else { headerView.tipsLableHeight.constant = 0; } [headerView whenTapWithBlock:^{ onlineTaskDetailModel.isSelected = !onlineTaskDetailModel.isSelected; YJTOnlineTaskDetailModel *detailModel = self.tempChapterListArray[section]; if (detailModel.subChapterList == nil) { detailModel.subChapterList = onlineTaskDetailModel.subChapterList; }else { detailModel.subChapterList = nil; } [self.tableView reloadData]; }]; return headerView; }
方法二:
上面的方法是通過控制模型數(shù)組來實現(xiàn)的,我們也可以采用控制界面的顯示,從而達到我們的要求。既然我們在點擊HeadView的時候已經(jīng)標記過對應的模型數(shù)據(jù)是否展開,那么我們完全可以通過控制界面對應分組的個數(shù)來實現(xiàn)。當然也可以通過控制rowHeight來到達效果。相比之下,該方法簡單明了些。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.chapterListArray[section]; return onlineTaskDetailModel.isSelected ? onlineTaskDetailModel.subChapterList.count : 0; } [headerView whenTapWithBlock:^{ onlineTaskDetailModel.isSelected = !onlineTaskDetailModel.isSelected; [self.tableView reloadData]; }];
總結(jié)
以上所述是小編給大家介紹的iOS 實現(xiàn)類似QQ分組樣式的兩種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
ios利用RunLoop原理實現(xiàn)去監(jiān)控卡頓實例詳解
這篇文章主要為大家介紹了ios利用RunLoop原理實現(xiàn)去監(jiān)控卡頓實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09詳解iOS AFNetworking取消正在進行的網(wǎng)絡請求
這篇文章主要介紹了詳解iOS AFNetworking取消正在進行的網(wǎng)絡請求,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06objective-c實現(xiàn)點到直線的距離及與垂足的交點
這篇文章主要給大家介紹了利用objective-c實現(xiàn)點到直線的距離及與垂足的交點的相關(guān)資料,文中給出了詳細的實現(xiàn)思路和實現(xiàn)代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04