简体   繁体   English

将.xib文件中TableCellView中项目的出口设置为自定义NSTableCellView子类

[英]Set the outlet of the item in TableCellView within the .xib file to the custom NSTableCellView subclass

I want to fill my NSTableView with content. 我想用内容填充我的NSTableView Per table-cell-row are 3 items (2 NSTextFields and 1 NSImageView ). 每个表单元行有3个项目(2个NSTextFields和1个NSImageView )。 For that I created a custom NSTableCellView where I want to set the @IBOutlets of the 3 Items, to set there the value for them. 为此,我创建了一个自定义NSTableCellView ,我想在其中设置3个项目的@IBOutlets并在其中设置它们的值。 But when I try to set the referencing outlets, the only option is to create an action. 但是,当我尝试设置引用出口时,唯一的选择是创建一个动作。

When I try to write @IBOutlet weak var personName: NSTextfield and then set the references, I can't because "xcode cannot locate the class in the current workspace" 当我尝试编写@IBOutlet weak var personName: NSTextfield并设置引用时,我无法执行此操作,因为“ xcode无法在当前工作空间中找到该类”

When I create the NSTableView inside a main.storyboard, I'm able to set the outlet references. 当我在main.storyboard内创建NSTableView ,可以设置出口引用。 So what is the different behavior between .storyboard and .xib? 那么.storyboard和.xib之间有什么不同的行为?

When I try to connect the @IBOutlet with the Item "Person Name" 当我尝试将@IBOutlet与项目“人员姓名”连接时 疑难排解

My NSViewController (owner of the .xib) 我的NSViewController(.xib的所有者)

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    @IBOutlet weak var tableView: NSTableView! //ref to tableView in xib
    var persons:[Person] = [] //content to fill tableview

    override func viewDidLoad() {
        super.viewDidLoad()

        persons.append(Person(name: "John", age: 23, piRef: "/Users/xy/Desktop/profilePic.png"))
        persons.append(Person(name: "Marie", age: 26, piRef: "/Users/xy/Desktop/profilePic.png"))


        tableView.delegate = self
        tableView.dataSource = self
    }


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

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        let tableCellView:personTableCell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "defaultRow"), owner: self) as! personTableCell
//NSTableColumn in xib has id "defaulRow"

        if let person:Person = persons[row] {

            tableCellView.setPerson(person: person) //call method inside NSTableCellView-subclass to set item values
        }

        return tableCellView        
    }    
}


The custom NSTableCellView subclass ( "personTableCell" ) 自定义NSTableCellView子类( “ personTableCell”

class personTableCell: NSTableCellView {

    var person:Person! = nil

    //here should be:
    //@IBOutlet weak var personName: NSTextField!
    //@IBOutlet weak var personAge: NSTextField!
    //@IBOutlet weak var personImg: NSImageView!

    func setPerson(person: Person) {

        self.person = person

        self.personName = person.name
        self.personAge = person.age
        self.personImg = NSImage(byReferencingFile: person.profileImgRef)

    }

}

I want to be able to add the item outlet references to my NSTableCellView-subclass . 我希望能够将项目出口引用添加到我的NSTableCellView-subclass中

It appears to me you're making this harder than it needs to be. 在我看来,您正在为此付出更多的努力。 makeView is giving you a reference to the cell. makeView为您提供了对该单元格的引用。 Therefore you can access its members directly. 因此,您可以直接访问其成员。 No need for outlets (which is why Xcode won't make them for you.) 无需插座(这就是Xcode不会为您制作插座的原因。)

I can't read your screenshots well enough to tell how the textfields are defined (old eyes), so I can only give you a generic example from a working demo of a custom cell class: 我的屏幕截图无法很好地理解文本字段的定义(老样子),因此我只能从自定义单元类的工作演示中为您提供一个通用示例:

class DIYTableViewDelegate: NSObject, NSTableViewDelegate {
    var count = 0   // counts the number of views actually created

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let id = tableColumn!.identifier
        var view = tableView.makeView(withIdentifier: id, owner: nil) as? CustomTableCellView
        if view == nil {
            view = createCell(id)
            count += 1
        }
        view!.textField!.stringValue = "\(id.rawValue) \(row) \(view!.count) \(count)"
        view!.count += 1

        return view
    }
}

Also, it's customary in Swift to capitalize the first letter of types (classes, structures, enums, protocols) and lowercase methods & properties. 另外,在Swift中习惯将大写的类型(类,结构,枚举,协议)的首字母以及小写的方法和属性大写。 Doesn't affect how your code compiles, but it helps other Swifties read it. 不会影响代码的编译方式,但可以帮助其他Swifties读取代码。

Here's another example that may help: 这是另一个可能有用的示例:

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        guard let vw = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? CustomTableCellView else { return nil }

        vw.textField?.stringValue = String(pictures[row].dropLast(4))
        vw.imageView?.image = NSImage(named: pictures[row])

        return vw
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM