SWIFT OS X - 预先选择tableView中的行不起作用

时间:2015-07-11 02:15:31

标签: macos swift nstableview

我在viewDidLoad()中使用下面的代码尝试在tableView对象(tblProjectNumber)中预先选择一个表行。编译或运行时没有错误。

一切都加载好了,我可以通过鼠标点击选择表格行,一旦加载视图就没有任何问题。但是,我无法加载具有预先选择的必需默认行的视图。我在viewDidLoad()中尝试了以下代码(最后三行引用)。 tableView不显示选定的行,并且在数据加载时不会在index(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中不可用,因此它对我也不起作用。

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?方法中尝试使用您的代码。

不知道哪种选择方法适用于OSX,但selectRowAtIndexPathiOS的正确方法。因此,尝试OSX的每个可能的selectIndex方法,但在viewForTableColumn方法中,通过该方法实际显示tabledata。即在显示tabledata ..时,将检查您要选择的索引。

希望这可能有所帮助!