iOS多級(jí)列表實(shí)現(xiàn)代碼
在項(xiàng)目開發(fā)中,層級(jí)列表經(jīng)常遇到,簡(jiǎn)單點(diǎn)的二級(jí)列表利用UITableView的Header就可以實(shí)現(xiàn),再簡(jiǎn)單點(diǎn)的三級(jí)列表通過(guò)對(duì)Cell高度進(jìn)行調(diào)整也可以實(shí)現(xiàn)三級(jí)列表的效果。但遇到多級(jí)列表,尤其是層次不明的動(dòng)態(tài)列表就比較麻煩了。
原理
層級(jí)列表和樹形結(jié)構(gòu)比較類似,不過(guò)不是二叉樹,而是多叉樹。每個(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。
我們可以通過(guò)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)信息通過(guò)返回。
dataSourceArr中是這樣的一種符合樹層級(jí)結(jié)構(gòu)的順序:

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

遇到問(wèn)題
1.局部刷新的問(wèn)題
每次展開或收攏以后刷新列表,一開始采用
但會(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í)有所幫助,也希望大家多多支持腳本之家。
- IOS實(shí)現(xiàn)展開二級(jí)列表效果
- IOS展開三級(jí)列表效果示例
- IOS實(shí)現(xiàn)簡(jiǎn)易版的QQ下拉列表
- iOS開發(fā)之在列表上方添加水印的方法
- iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼
- ios基于UITableViewController實(shí)現(xiàn)列表
- iOS實(shí)現(xiàn)列表折疊效果
- iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)的問(wèn)題解決
- iOS實(shí)現(xiàn)從通訊錄中選擇聯(lián)系人
- iOS實(shí)現(xiàn)聯(lián)系人列表功能
相關(guān)文章
iOS開發(fā)中UITabBarController的使用示例
這篇文章主要介紹了iOS開發(fā)中UITabBarController的使用示例,代碼基于Objective-C進(jìn)行演示,需要的朋友可以參考下2015-09-09
解決iOS下無(wú)法觸發(fā)focus事件的問(wèn)題
今天小編就為大家分享一篇解決iOS下無(wú)法觸發(fā)focus事件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼
本文是腳本之家小編給大家分享的iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-09-09

