美文网首页
Mac Dev Part 3 - NSOutlineView

Mac Dev Part 3 - NSOutlineView

作者: 遇见Miu | 来源:发表于2019-03-04 20:38 被阅读0次

其实周末也不能懈怠啊

实现类似于QQ好友展开列表,发现Mac上实现还是很麻烦的

1.拖入SourceList, 创建根叶模型

RootModel.swift

class RootModel: NSObject {
    
    var name = ""
    // 是否是子节点
    var isLeaf = false
    // children为子节点数组,也可以起其他名称
    var children = [LeafModel]()
}

LeafModel.swift

class LeafModel: NSObject {
    var leafName = ""
    var hasChildren = false
    init(name: String) {
        self.leafName = name
    }
}

2.添加数据

ViewController.swift

    func addData() {
        let leaf1 = LeafModel(name: "艾希")
        let leaf2 = LeafModel(name: "易")
        let leaf3 = LeafModel(name: "泰达米尔")
        let root1 = RootModel()
        root1.name = "LOL"
        root1.children = [leaf1, leaf2, leaf3]
        
        let leaf4 = LeafModel(name: "北京")
        let leaf5 = LeafModel(name: "上海")
        let leaf6 = LeafModel(name: "深圳")
        let root2 = RootModel()
        root2.name = "城市"
        root2.children = [leaf4, leaf5, leaf6]
    
        rootArray = [root1, root2]
        self.outlineView .reloadData()
    }

查看的demo并不需要reloadData一下,但是这样写逻辑是对的,以后再碰到再看看

3.连接Delegate和DataSource,实现代理和数据源

ViewController.swift


// MARK: DataSource
extension ViewController {

    // item的数量
    func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
        if let model = item as? RootModel {
            return model.children.count
        } else {
            return rootArray.count
        }
    }
    
    // 是否能够展开
    func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
        return item is RootModel
    }
    
    // item的数据源
    func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
        if let model = item as? RootModel {
            return model.children[index]
        } else {
            return rootArray[index]
        }
    }
}

// MARK: Delegate
extension ViewController {

    // itemView的显示,相当于iOS中的cell
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        var cell: NSTableCellView?
        if item is RootModel {
            let model = item as? RootModel
            cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HeaderCell"), owner: self) as? NSTableCellView
            cell?.textField?.stringValue = model?.name ?? ""
        } else {
            let model = item as? LeafModel
            cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"), owner: self) as? NSTableCellView
            cell?.textField?.stringValue = model?.leafName ?? ""
        }
        return cell
    }
}


效果:


效果图.gif

4.默认展开

ViewController.swift

outlineView.expandItem(nil, expandChildren: true)

相关文章

  • Mac Dev Part 3 - NSOutlineView

    其实周末也不能懈怠啊实现类似于QQ好友展开列表,发现Mac上实现还是很麻烦的 1.拖入SourceList, 创建...

  • Mac Dev Part 4 - NSOutlineView -

    自己选择的路,怎么都得走下去swift不熟悉,用的是另外一种思路,所以遇到问题有点束手无策 1.剪切板相关 2.U...

  • Mac Dev Part 6 - ContainerView &

    时间是挤出来的感觉需要加快点脚步才行呀,时间感觉总是不够用 1.ContainerView的创建 Containe...

  • Mac Dev Part 7 - Notification

    生活不易但是不能让它慢慢磨掉你向往美好的蓝图 1.NSNotification 和iOS上的通知没有什么区别,所以...

  • Mac Dev Part 9 - WindowViewContr

    时间是挤出来的我觉得每个Part可以多写点东西,这样不至于会写很多Part WindowController的to...

  • Mac Dev Part 8 - NSPopover

    今天周五,好好干活女生过节,不关我啥事= = 按钮弹出PopoverViewController,点击加号能够进行...

  • Mac Dev Part 10 - SplitViewContr

    最难的还是坚持啦吃完饭好好把今天的学习任务完成 偏好设置 将偏好设置和目标控制器进行连接(show的方式跳转),其...

  • Mac Dev Part 11 - NSToolbar

    最难的还是坚持啦好好工作好好码代码 NSToolbar 在Toolbar上拖入一个label,加入item spa...

  • Mac Dev Part 5 - NSAlertView &am

    时间挤的话,总是会有的比如说需求做完的时候,等同事接入的时候 1.NSAlert的两种弹出方式 首先创建NSAle...

  • Mac Dev Part 1 - Menu Bar

    时间总是挤出来的从今天开始进行记录和学习Mac开发 1.展示名为statusItembutton的图片到Mac的状...

网友评论

      本文标题:Mac Dev Part 3 - NSOutlineView

      本文链接:https://www.haomeiwen.com/subject/nuswuqtx.html