【问题标题】:Populating NSTableView from array in Swift (without XCode)在 Swift 中从数组中填充 NSTableView(没有 XCode)
【发布时间】:2016-05-09 09:29:24
【问题描述】:

我正在尝试在不使用 Swift 中的 XCode 的情况下为 OS X 进行开发。我在尝试从任何类型的数据源填充 NSTableView 时遇到了极大的麻烦。这是我的代码:

import Cocoa

class StupidDataSource: NSObject, NSTableViewDataSource, NSTableViewDelegate {

    var lib:[NSDictionary] = [["a": "1", "b": "2"],
                              ["a": "3", "b": "4"]]

    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return lib.count
    }

    func tableView(tableView: NSTableView,
                   objectValueForTableColumn tableColumn: NSTableColumn?,
                   row: Int) -> AnyObject? {
        let result = lib[row].objectForKey(tableColumn!.identifier)
        return result
    }
}


func make_table(window: NSWindow,
                _ size:(x:Int, y:Int, width:Int, ht:Int),
                _ title:String,
                _ data:StupidDataSource
    )-> NSTableView {
    let tableContainer = NSScrollView(frame:NSMakeRect(0, 0, 400, 400))
    let tableView = NSTableView(frame: NSMakeRect(0, 0, 400, 400))
    tableView.setDataSource(data)
    tableView.reloadData()
    tableContainer.documentView = tableView
    tableContainer.hasVerticalScroller = true
    window.contentView!.addSubview(tableContainer)
    return tableView
}

class AppDelegate: NSObject, NSApplicationDelegate {
    let window = NSWindow()

    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        set_window_args(window, 400, 400, "Ass")
        let dumb_dict = StupidDataSource()
        let tableytable = make_table(window, (100, 100, 0 ,0), "poop", dumb_dict)

        window.makeKeyAndOrderFront(window)
        window.level = 1
    }
}

let app = NSApplication.sharedApplication()
app.setActivationPolicy(.Regular)

let controller = AppDelegate()

app.delegate = controller
app.run()

这可以在命令行使用“swift file.swift”运行。我已经实现了 NSTableViewDataSource 所需的方法,并根据 Apple 的文档正确初始化了所有内容,但表格中没有显示任何内容。缺少什么?

【问题讨论】:

  • 你在哪里设置表格列标识符?如果未设置,您的数据源将返回 nil。

标签: xcode swift macos nsdictionary nstableview


【解决方案1】:

我不是专家,但在我看来,您的 tableView 函数只是从字典中返回相关行的内容。

我个人将一个单元格作为 NSView 返回?喜欢:-

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
    if let cell = myViewBasedTableView.makeViewWithIdentifier(tableColumn!.identifier, owner: nil) as? NSTableCellView
    {
        cell.textField!.stringValue = "Hello";
        // Instead of "Hello". Put the contents of one element from your
        // dict in here. This func is called automatically for every cell
        // in the grid.

        return cell;
    }
    return myEmptyDefaultCell;
}

话虽如此,也许您的示例对于基于单元格的 tableView 是正确的。我从来没有用过其中之一,所以不知道。我总是使用基于视图的表格视图。我经常在单元格中放置小图像/图标以及使用以下代码的文本:

cell.textField!.stringValue = "Hello";
cell.imageView!.image = smileyFace;
return cell;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多