傲视苍穹iOS《Objective-C》VIP专题iOS点点滴滴iOS总结

iOS | UITableView简单实现一种树状展示视图

2017-09-26  本文已影响151人  清無

本例提供了一种“最简单”的利用UITableView实现树状结构的方法和思路,适用于OC和Swift。

最终效果

思路

[
    {
        "name": "1",
        "subs": [
            {
                "name": "1.1",
                "subs": [
                    {
                        "name": "1.1.1",
                        "subs": []
                    }
                ]
            }
        ]
    }
]
class TreeNode: NSObject {
    var name = ""
    var isOpen = false
    var subNodes = [TreeNode]()
    var levelString = ""
    
    var level: Int{
        return levelString.components(separatedBy: ".").count
    }
    var needsDisplayNodes: [TreeNode]{
        return needsDisplayNodesOf(ancestor: self)
    }
    var isLeaf: Bool{
        return subNodes.isEmpty
    }
}

实现

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    
    let node = dataSource[indexPath.row]
//1. 点击叶子节点,不再展开/插入
    if node.isLeaf {
        return
    }
    
    node.isOpen = !node.isOpen
    let nodes = node.needsDisplayNodes
    let insertIndex = dataSource.index(of: node)! + 1
//2. 插入subNodes - 展开
    if node.isOpen {
        dataSource.insert(contentsOf: nodes, at: insertIndex)
        tableView.insertRows(at: nodes.map{
            IndexPath(row: dataSource.index(of: $0)!, section: 0)
        }, with: .top)
    }
//3. 移除所有的subNodes - 收起
    else{
        for subNode in nodes {
            guard let index = dataSource.index(of: subNode) else {
                continue
            }
            dataSource.remove(at: index)
            tableView.deleteRows(at: 
                [IndexPath(row: index, section: 0)]
            , with: .bottom)
        }
    }
//4. 刷新当前cell的状态 - 卷/展icon
    tableView.reloadRows(at: [indexPath], with: .none)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let node = dataSource[indexPath.row]
    let flagText = !node.isLeaf ? (node.isOpen ? "-" : "+") : ""
    
    var cell: UITableViewCell!
//1. 叶子节点cell
    if node.isLeaf {
        cell = tableView.dequeueReusableCell(withIdentifier: "TreeLeafCell", for: indexPath) as! TreeLeafCell
    }
    else{
//2. 正常节点cell
        cell = tableView.dequeueReusableCell(withIdentifier: "TreeNodeCell", for: indexPath) as! TreeNodeCell
    }
    
    cell.textLabel?.text = String(repeating: "    ", 
                                  count: (node.level > 1 ? node.level : 0)) 
        + "\(flagText)  " + node.name
    
    return cell
}
extension TreeNode{ 
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        if key == "subs", let subs = value as? [[String: Any]]{
            for i in 0..<subs.count {
                let tree = TreeNode.modelWithDictionary(subs[i], levelString: i,parent: levelString)
                subNodes.append(tree)
            }
        }
    }
    
    public static func modelWithDictionary(_ dict: [String: Any], levelString index: Int, parent levelString: String?) -> TreeNode{
        let model = TreeNode()
        model.levelString = levelString != nil ? (levelString! + ".\(index + 1)") : "\(index + 1)"
        model.setValuesForKeys(dict)
        return model
    }
}
extension TreeNode{
    var needsDisplayNodes: [TreeNode]{
        return needsDisplayNodesOf(ancestor: self)
    }
    
    // 应该显示的
    private func needsDisplayNodesOf(ancestor: TreeNode) -> [TreeNode]{
        var nodes = [TreeNode]()
        for node in ancestor.subNodes {
            nodes.append(node)
            if node.isOpen {
                nodes.append(contentsOf: needsDisplayNodesOf(ancestor: node))
            }
        }
        return nodes.sorted{ $0.levelString < $1.levelString }
    }
}
extension TreeNode{
    public static func mockData() -> [TreeNode]{
        var trees = [TreeNode]()
        do{
            let data = try Data(contentsOf: Bundle.main.url(forResource: "tree.json", withExtension: nil)!)
            guard let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [[String: Any]] else{
                return trees
            }
            for i in 0..<jsonArray.count{
                let tree = TreeNode.modelWithDictionary(jsonArray[i], levelString: i, parent: nil)
                trees.append(tree)
            }
        }catch{
            fatalError("JSON数据解析失败")
        }
        return trees
    }
}

优缺点

思路清晰,逻辑简单,代码实现容易;
代码执行效率高;
实现了上次卷展状态记忆;

代码对Data Model的依赖性较强;
没有实现封装;

GitHub

https://github.com/BackWorld/TreeTableView
如果对你有帮助,别忘了点个❤️并关注下我哦。

上一篇下一篇

猜你喜欢

热点阅读