在Swift中的源列表(NSOutlineView)中显示字符串列表 [英] Show list of strings in Source List (NSOutlineView) in Swift

查看:314
本文介绍了在Swift中的源列表(NSOutlineView)中显示字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在源列表侧边栏中显示一个简单的字符串列表 - 类似于Finder或Github应用程序。通过阅读协议参考,我可以'看看哪个方法设置视图显示的内容。到目前为止我有:

I'm trying to show a simple list of strings in a source list sidebar - similar to that in Finder or the Github app. From reading the protocol reference I can't see which method is setting what the view displays. So far I have:

var items: [String] = ["Item 1", "Item 2", "Item is an item", "Thing"]
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
    return items[index]
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
    return false
}
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
    if item == nil {
        return items.count
    }
    return 0
}
func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
    return "ITEM"
}
func outlineView(outlineView: NSOutlineView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) {
    println(object, tableColumn, item)
}
// Delegate
func outlineView(outlineView: NSOutlineView, dataCellForTableColumn tableColumn: NSTableColumn?, tem item: AnyObject) -> NSCell? {
    println("Called")
    let view = NSCell()
    view.stringValue = item as String
    return view
}

我得到的是一个包含四个空白项目(无文本)的源列表。是否需要重写NSOutlineViewDelegate中的另一个方法来显示信息?

And all I get is a source list with four blank items (No text). Do I need to override another method from the NSOutlineViewDelegate to show the information?

推荐答案

如果您愿意使用基于视图的大纲视图,而不是基于单元格的,您可以替换委托方法 outlineView:dataCellForTableColumn:item ,其视图 outlineView:viewForTableColumn:item:

If you're happy to use a view-based outline view, rather than a cell-based one, you can replace the delegate method outlineView:dataCellForTableColumn:item, with its view equivalent outlineView:viewForTableColumn:item:

func outlineView(outlineView: NSOutlineView,
    viewForTableColumn tableColumn: NSTableColumn?,
    item: AnyObject) -> NSView? {

    var v = outlineView.makeViewWithIdentifier("DataCell", owner: self) as NSTableCellView
    if let tf = v.textField {
        tf.stringValue = item as String
    }
    return v
} 

请注意,这个方法是 NSTableView 方法 makeViewWithIdentifier:owner:。此方法的第一个参数 - 字符串 DataCell - 是标识符 code> NSTableViewCell 对象,当您将它拖动到画布上时,它会自动插入到 NSOutlineView 中。此对象具有 textField 属性和 imageView ;所有你需要做的是将 textField stringValue 属性设置为项的值

Note that the important call within this method is the NSTableView method makeViewWithIdentifier:owner:. The first argument to this method - the string DataCell - is the value of the identifier Interface Builder gives to the NSTableViewCell object that it automatically inserts into your NSOutlineView when you drag it onto the canvas. This object has a textField property, and an imageView; all you need to do is set the stringValue property of the textField to the value of item.

这篇关于在Swift中的源列表(NSOutlineView)中显示字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆