如何解决我正在尝试创建一个带有一个子项的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 举报,一经查实,本站将立刻删除。