xcode - 在 Swift 中从数组填充 NSTableView(没有 XCode)

标签 xcode swift macos nsdictionary nstableview

我正在尝试在 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 的文档正确初始化了所有内容,但表中没有显示任何内容。缺少什么?

最佳答案

我不是专家,但在我看来,您的 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 是正确的。我从未使用过其中之一,所以不知道。我总是使用 viewBased TableView 。我经常使用以下代码在单元格中放置小图像/图标以及文本:

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

关于xcode - 在 Swift 中从数组填充 NSTableView(没有 XCode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121913/

相关文章:

iPhone 应用程序构建到模拟器,不会构建到设备

ios - 翠鸟不显示图像

macos - 如何从可恢复的 NSWindow 中排除某些 AppKit View ?

linux - 从入口点动态选择在主机上运行 Docker 的用户 GUI 和 UID

ios - 什么是 iDOT block

ios - 获取 UnityException : Launching iOS project via Xcode4 failed

ios - 上传到 iOS 应用商店时出现错误 "your binary file is invalid"

ios - Xcode 和 Flutter 插件 : Could not find or use auto-linked framework 'Flutter'

swift - 为什么在分段控件上使用 UIControlState.normal 会导致此崩溃?

ios - Spotify iOS SDK Swift 显示所有(!)播放列表(20+)