0
參考附上的圖片。 斯威夫特3表列寬最後一列不工作
請注意,由於某種原因,最後一列在寬度上總是很短。我不能爲了我的生活找出爲什麼或如何解決這個問題?
這是我爲我的控制器代碼。
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($0)
}
//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()
}
}
好的,讓我試試看。 –
非常感謝你設置了這個技巧,self.theTableview?.columnAutoresizingStyle = .noColumnAutoresizing –