2017-06-10 30 views
0

參考附上的圖片。 enter image description here斯威夫特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() 
    } 

} 

回答

0

NSTableColumn有一個屬性resizingMaskNSTableView有一個屬性columnAutoresizingStyle。兩者都可以在IB或代碼中設置。找出一個配置,使列的行爲像你想要的。 IB中表格視圖的默認列大小是'僅限最後一列',切換到'無'將解決您的問題。

另一個解決方案是設置列minWidth

+0

好的,讓我試試看。 –

+0

非常感謝你設置了這個技巧,self.theTableview?.columnAutoresizingStyle = .noColumnAutoresizing –