如何从 NIB 创建自定义 NSTableCellView? [英] How to create a custom NSTableCellView from a NIB?

查看:21
本文介绍了如何从 NIB 创建自定义 NSTableCellView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 的新手,正在为 NSTableView 苦苦挣扎!我正在尝试从 NIB 创建自定义 NSTableCellView.

I'm new to Swift and I am struggling with NSTableView! I'm trying to create a custom NSTableCellView from a NIB.

我想从 NIB 加载单元格,因为:

I want to load the cell from a NIB because:

  • 它将在多个列和多个表视图中重复使用
  • 它将在视觉和功能上(相对)复杂
  • 它可能会在开发过程中演变

我可以在我的表格视图中加载单元格,但我收到无法连接出口...缺少设置器或实例变量";当我尝试用数据填充视图时,调试区域中出现错误.自定义单元格在 tableview 中可见,但似乎没有被实例化.

I'm able to load the cell in my table view but I'm getting a "Failed to connect outlet from... missing setter or instance variable" error in the debug area when I try to populate the view with data. The custom cell is visible in the tableview but doesn't seem to be instantiated.

我已经在网上搜索了几个小时的解决方案!帮助我缺少什么?

I've searched for a solution online for hours! Help what am I missing?

这是我的 TableViewController...

This is my TableViewController...

protocol TableViewDelegate {
    func itemWithIndexWasSelected(value: Int)
}

class TableViewController: NSViewController {

    @IBOutlet weak var tableView: NSTableView!

    let tableViewData =
        [  [ "Column1": "John", "Column2": "Smith", "Hobby": "Birds"],
           [ "Column1": "Jane", "Column2": "Doe", "Hobby": "Fish"],
           [ "Column1": "Hal", "Column2": "Bernard", "Hobby": "Trees"],
           [ "Column1": "Harry", "Column2": "Bell", "Hobby": "Rocks"] ]


    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    
        let customCellNib = NSNib.init(nibNamed: "CustomTableCellView", bundle: nil)
        tableView.register(customCellNib, forIdentifier: NSUserInterfaceItemIdentifier("CustomCellView"))
        tableView.reloadData()
    } 
}

extension TableViewController: NSTableViewDataSource, NSTableViewDelegate {

    func numberOfRows(in tableView: NSTableView) -> Int {
        return tableViewData.count
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    
        if tableColumn?.identifier.rawValue == "CustomCell" { 
            let result: CustomTableCellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CustomCellView"), owner: self) as! CustomTableCellView
        
            result.hobbyLabel?.stringValue = tableViewData[row]["Hobby"]!
            result.hobbyButton?.title = "TESTTITLE"
        
            return result
        }

        else {
            let result = tableView.makeView(withIdentifier:(tableColumn?.identifier)!, owner: self) as! NSTableCellView
            result.textField?.stringValue = tableViewData[row][(tableColumn?.identifier.rawValue)!]!
        return result
        }
    } 
}

我有一个带有相同名称的 XIB 的 CustomTableCellView...

I have a CustomTableCellView with a XIB that has the same name...

class CustomTableCellView: NSTableCellView {

    @IBOutlet weak var hobbyButton: NSButton!
    @IBOutlet weak var hobbyLabel: NSTextField!

}

我有一个可以发送或上传的测试项目……非常感谢您的帮助!

I have a test project that I can send or upload... help will be hugely appreciated!

这是我看到的:

推荐答案

编辑 xib 时,File's Owner 代理对象代表作为所有者传递给 makeView(withIdentifier:owner:) 的对象在运行时.在这种情况下,所有者对象是视图控制器.您可以将 xib 中 File's Owner 的类设置为 TableViewController 并连接操作.您不能将文件所有者的类设置为 CustomTableCellView 并连接出口.而是连接 CustomTableCellView 视图对象的出口.

When editing a xib, the File's Owner proxy object represents the object which is passed as owner to makeView(withIdentifier:owner:) at runtime. In this case the owner object is the view controller. You can set the class of the File's Owner in the xib to TableViewController and connect actions. You can't set the class of the File's Owner to CustomTableCellView and connect outlets. Instead connect the outlets of the CustomTableCellView view object.

这篇关于如何从 NIB 创建自定义 NSTableCellView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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