Swift 如何在NSTableCellView中对齐对象

Swift 如何在NSTableCellView中对齐对象,swift,xcode,Swift,Xcode,我尝试将NSpoupButton垂直放置在表格单元格的顶部,但没有成功。它保持垂直居中: 返回NSPoupButtons的代码: extension ViewController:NSTableViewDataSource, NSTableViewDelegate{ func tableViewColumnDidResize(_ notification: Notification) {…} func tableView(_ tableView: NSTableView, height

我尝试将NSpoupButton垂直放置在表格单元格的顶部,但没有成功。它保持垂直居中:

返回NSPoupButtons的代码:

extension ViewController:NSTableViewDataSource, NSTableViewDelegate{


func tableViewColumnDidResize(_ notification: Notification) {…}


func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {…}


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

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

    if tableColumn?.identifier.rawValue == "value" {

        …

    } else if (tableColumn?.identifier.rawValue == "type") {

        let typePopUp = NSPopUpButton()

        typePopUp.addItems(withTitles:["Data", "String", "Number", "Boolean"])
        typePopUp.isBordered = false

        return typePopUp

    } else {…}
}
}


有人能给我指出正确的方向吗?

我建议您对该列的表格单元格进行子类化,将按钮嵌入到NSView中,并在子类中添加“customizeCell”函数。 当从委托调用表行时,该函数可以找到表行的实际行高,相应地调整视图的大小,并使用autolayout相对于视图定位按钮

另外,虽然我不认为这会导致您的问题,除非您为了简洁起见将其排除在问题之外,否则您不会使用“makeView”

通常

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{
从以下位置获取其单元格:

let vw = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self)
Cocoa从nib构建单元,并将其排队以供重用(这可能只在您的表很长时才起作用)


希望我在这里走上正轨。

使用自动调整大小掩码?您是否在InterfaceBuilder中使用自动布局?你是如何定位控件的?我禁用了AutoLayout,但id没有起作用。如果我对弹出窗口应用约束,它会给我一个运行时错误:[Layout]无法同时满足约束:(“”,“”)代码:NSLayoutConstraint.activate([typePopUp.heightAnchor.constraint(equalToConstant:17)])谢谢Ron,子类化/嵌入成功了!与makeView完美配合。这正是我所需要的。干杯,蒂姆