我正在尝试创建一个带有一个子项的NSOutlineView,但子项文本未显示

问题描述

我试图用一个孩子创建一个NSOutlineView,我在视图控制器文件中创建了结构,并且根节点文本显示出来,但是在子节点上没有文本

下面是随后添加到列表中的结构,我希望将新的注释文本添加到注释部分下,并将新的标签添加到浏览器部分下,依此类推

我尝试了不同的方法,但是代码似乎没有执行。如果能对我轻松一点,我将不胜感激。我最近刚从这个星期开始使用StoryBoard和Xcode

如果能给我帮助,我将不胜感激。谢谢 下面是我的ViewController文件中的代码

import Cocoa

struct Sections {
    var name: String
    var items: Int
}

struct SubSections {
    var name: String
}

class ViewController: NSViewController,NSOutlineViewDelegate,NSOutlineViewDataSource {
    
    
    
    @IBOutlet weak var outlineView: NSOutlineView!
    
    
    
    
    
    var subKeys = [SubSections]()
    var keys = [Sections]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        keys.append(Sections(name: "Notes",items: 1))
        keys.append(Sections(name: "Tabs",items: 1))
        keys.append(Sections(name: "Versions",items: 1))
        subKeys.append(SubSections(name: "Add New Note"))
        subKeys.append(SubSections(name: "Add New browser Tab"))
        subKeys.append(SubSections(name: "Add New Version"))
        outlineView.delegate = self
        outlineView.dataSource = self

        
    }
    
    func outlineView(_ outlineView: NSOutlineView,child index: Int,ofItem item: Any?) -> Any {
        if item == nil {
            return keys[index]
        }
        else{
            return 1
        }
        
    }

    func outlineView(_ outlineView: NSOutlineView,isItemExpandable item: Any) -> Bool {
        if let section = item as? Sections {
            return section.items > 0
          }
        else if item is SubSections {
            return false
        }
        return false
    }
 
    
    func outlineView(_ outlineView: NSOutlineView,numberOfChildrenOfItem item: Any?) -> Int {
        if item == nil {
            return keys.count
        }
        else {
            return 1
        }

    }
    
            
    func outlineView(_ outlineView: NSOutlineView,viewFor tableColumn: NSTableColumn?,item: Any) -> NSView? {
        var view: NSTableCellView?
        if let section = item as? Sections {
          //2
            view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"),owner: self) as? NSTableCellView
          if let textField = view?.textField {
            //3
            textField.stringValue = section.name
            textField.sizetoFit()
          }
        }
        else if let subsection = item as? SubSections {
          //2
            view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "itemCell"),owner: self) as? NSTableCellView
          if let textField = view?.textField {
            //3
            textField.stringValue = subsection.name
            textField.sizetoFit()
          }
        }
        return view
    }


    
   

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)