国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS多級(jí)列表實(shí)現(xiàn)代碼

2020-07-26 03:13:48
字體:
供稿:網(wǎng)友

在項(xiàng)目開發(fā)中,層級(jí)列表經(jīng)常遇到,簡(jiǎn)單點(diǎn)的二級(jí)列表利用UITableView的Header就可以實(shí)現(xiàn),再簡(jiǎn)單點(diǎn)的三級(jí)列表通過對(duì)Cell高度進(jìn)行調(diào)整也可以實(shí)現(xiàn)三級(jí)列表的效果。但遇到多級(jí)列表,尤其是層次不明的動(dòng)態(tài)列表就比較麻煩了。

原理

層級(jí)列表和樹形結(jié)構(gòu)比較類似,不過不是二叉樹,而是多叉樹。每個(gè)節(jié)點(diǎn)只需要擁有指向父節(jié)點(diǎn)和子節(jié)點(diǎn)的兩個(gè)指針,就能形成一顆樹。我們將多級(jí)列表中每一級(jí)對(duì)象看作一個(gè)node,node擁有兩個(gè)屬性,分別為父節(jié)點(diǎn)和子節(jié)點(diǎn)的ID。

每棵樹有個(gè)一個(gè)虛擬的root節(jié)點(diǎn),它的ID為rootID,所有節(jié)點(diǎn)中凡是父節(jié)點(diǎn)ID為rootID的便是第一級(jí),對(duì)應(yīng)樹結(jié)構(gòu)中的depth(深度)。這樣每一個(gè)node對(duì)象就都擁有了parentID和childrenID, childrenID為node對(duì)象的ID。

我們可以通過rootID查出第一級(jí)node,再根據(jù)第一級(jí)node的childrenID查出下一級(jí),依次類推,確定所有節(jié)點(diǎn)的父子關(guān)系。同時(shí)也可以確定葉子節(jié)點(diǎn)和第一級(jí)節(jié)點(diǎn),也可稱

為根節(jié)點(diǎn)。

效果圖

1.一般多級(jí)列表

2.記錄節(jié)點(diǎn)歷史狀態(tài)的列表

思路

1.首先根據(jù) rootID 獲取所有第一級(jí)節(jié)點(diǎn),并放入U(xiǎn)ITableView的數(shù)據(jù)源 dataSourceArr 中,展示初始化列表

2. 展開: 點(diǎn)擊節(jié)點(diǎn)cell,根據(jù) childrenID 查找下一級(jí)nodes,并插入到 dataSourceArr 中currentNode的后面,刷新展示

3. 收攏: 點(diǎn)擊以打開節(jié)點(diǎn)cell,從 dataSourceArr 的CurrentIndex+1開始,如果該節(jié)點(diǎn)的level小于currentNode的level,則移除node,否則停止刷新列表。

4.點(diǎn)擊cell為葉子節(jié)點(diǎn)則不響應(yīng)展開或收攏操作,并把節(jié)點(diǎn)信息通過返回。

dataSourceArr中是這樣的一種符合樹層級(jí)結(jié)構(gòu)的順序:

定義節(jié)點(diǎn)對(duì)象

遇到問題

1.局部刷新的問題

每次展開或收攏以后刷新列表,一開始采用

復(fù)制代碼 代碼如下:
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

但會(huì)導(dǎo)致節(jié)目有整體閃爍的效果,體驗(yàn)不好。最后考慮采用局部刷新 insertRowsAtIndexPaths 和 deleteRowsAtIndexPaths 。

但在刷新中會(huì)報(bào)錯(cuò)

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 0 which only contains 2 rows before the update'

推測(cè)原因是 current Cell在刷新時(shí)的numberOfRowsInSection和刷新insert or del的cell時(shí)numberOfRowsInSection不一致導(dǎo)致 。然后嘗試current cell和其他cell分別刷新,完美刷新。

[_reloadArray removeAllObjects]; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; if (currentNode.isExpand) {  //expand  [self expandNodesForParentID:currentNode.childrenID insertIndex:indexPath.row];  [tableView insertRowsAtIndexPaths:_reloadArray withRowAnimation:UITableViewRowAnimationNone]; }else{  //fold  [self foldNodesForLevel:currentNode.level currentIndex:indexPath.row];   [tableView deleteRowsAtIndexPaths:_reloadArray withRowAnimation:UITableViewRowAnimationNone]; }

2.怎么保存節(jié)點(diǎn)歷史狀態(tài)

當(dāng)文件級(jí)層比較多時(shí),有時(shí)希望能關(guān)掉層級(jí)后再打開時(shí)還能保留子層級(jí)的打開狀態(tài)。我們可以會(huì)給每一個(gè)node一個(gè)是否展開的屬性,當(dāng)fold時(shí)只修改currentNode的expand屬性,expand時(shí)對(duì)子節(jié)點(diǎn)序isexpand=Y(jié)ES的進(jìn)行遍歷插入。

//expand- (NSUInteger)expandNodesForParentID:(NSString*)parentID insertIndex:(NSUInteger)insertIndex{ for (int i = 0 ; i<_nodes.count;i++) {  YKNodeModel *node = _nodes[i];  if ([node.parentID isEqualToString:parentID]) {   if (!self.isPreservation) {    node.expand = NO;   }   insertIndex++;   [_tempNodes insertObject:node atIndex:insertIndex];   [_reloadArray addObject:[NSIndexPath indexPathForRow:insertIndex inSection:0]];//need reload nodes   if (node.isExpand) {    insertIndex = [self expandNodesForParentID:node.childrenID insertIndex:insertIndex];   }  } } return insertIndex;}

demo地址:
https://github.com/YangKa/YKMutableLevelTableView.git

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥化市| 威信县| 五寨县| 康乐县| 弥渡县| 平远县| 修文县| 浙江省| 包头市| 海伦市| 盖州市| 九龙城区| 姜堰市| 保亭| 香港| 钟山县| 斗六市| 英吉沙县| 宾川县| 穆棱市| 斗六市| 牡丹江市| 武清区| 安丘市| 攀枝花市| 永和县| 舞钢市| 申扎县| 曲阳县| 疏勒县| 台南县| 固原市| 高碑店市| 大同市| 屏山县| 兴宁市| 正定县| 中阳县| 长治市| 河津市| 额济纳旗|