Swift 3 Table 列宽最后一列不起作用

Swift 3 Table Column Width Last Column Not Working

参考我附上的图片。

请注意,由于某种原因,最后一列的宽度总是很短。我这辈子都想不出为什么或如何解决这个问题?

这是我的控制器代码。

import Cocoa

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {

    @IBOutlet weak var theTableview: NSTableView!

    var data:NSArray = [""] //@JA - This is used

    override func viewDidLoad() {
        super.viewDidLoad()

        //First remove all columns
        let columns = self.theTableview.tableColumns
        columns.forEach {
            self.theTableview.removeTableColumn([=12=])
        }

        //self.theTableview?.columnAutoresizingStyle = .sequentialColumnAutoresizingStyle

        for index in 0...100 {
            let column = NSTableColumn(identifier: "defaultheader")
            if(index != 0){
                column.title = "Month \(index)"
            }else{
                column.title = "Factors"
            }

            self.theTableview.addTableColumn(column)
        }



        // Do any additional setup after loading the view.
        data = ["Group 1","Group 2","Group 3","Group 4"]

        self.theTableview.reloadData()
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

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

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        if let cell = tableView.make(withIdentifier: "defaultcell", owner: nil) as? NSTableCellView {
            cell.textField?.stringValue = data.object(at: row) as! String
            return cell
        }
        return nil
    }

    @IBAction func startsimulation(_ sender: NSButton) {
        //Recalculates the data variable for updating the table.

        data = ["group1","group2"]

        theTableview.reloadData()
    }

}

NSTableColumn 有一个 属性 resizingMaskNSTableView 有一个 属性 columnAutoresizingStyle。两者都可以在 IB 或代码中设置。找出一个配置,使列的行为符合您的要求。 IB 中 table 视图的默认列大小为 'Last Column Only',切换到 'None' 将解决您的问题。

另一个解决方案是设置 minWidth 列。