我在viewDidLoad()中使用下面的代碼來嘗試預先選擇tableView對象(tblProjectNumber)中的錶行。編譯或運行時沒有錯誤。SWIFT OS X - 在tableView中預選行不工作
一切都加載好了,我可以通過點擊鼠標來選擇表格行,沒有任何問題,一旦加載視圖。但是,我無法使用預先選擇的所需默認行加載視圖。我已經在viewDidLoad()(最後三行參考)中嘗試了以下代碼。 tableView不會顯示選定的行,並且在數據加載時不會在索引(100)處滾動該行。
override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.windowBackgroundColor().CGColor
self.view.window?.title = "Project Detail Module"
self.view.window?.titleVisibility
// sort the list of project numbers so that they display correctly when the view is first loaded
self.sortProjectNumberArray()
self.loadProjectRowDataArray()
self.sortPPRFilesForLatestDate()
// pre-select table row to load on initial display of the tableView
let index = NSIndexSet(index: 100)
self.tblProjectNumber.scrollRowToVisible(100)
self.tblProjectNumber.selectRowIndexes(index, byExtendingSelection: true)
}
答案在這裏:Select row in tableView programmatically by other method beside calling their indexPath
這裏:Select tableview row programmatically
這裏:Xcode - Swift Programatically select tablecell
有幫助,但我一直沒能解決問題。此外,在最後一個鏈接中使用的selectRowAtIndexPath調用在Swift中似乎不可用,因此它對我也不起作用。
任何協助讚賞。
這樣做。我將相關代碼添加到我的 'tableView(tableView:NSTableView,objectValueForTableColumn tableColumn:NSTableColumn?,row:Int) - > AnyObject? {}' – Wolfstar
很高興幫助:) – iRiziya