2015-07-11 166 views
0

我在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中似乎不可用,因此它对我也不起作用。

任何协助赞赏。

回答

1

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

不知道哪个选择方法将工作在OSXselectRowAtIndexPathiOS正确的方法。因此,尝试使用OSX的每个可能的selectIndex方法,但在方法中,通过它实际显示表数据。即当显示tabledata ..它将检查你想要选择的索引。

希望这可以帮助!

+0

这样做。我将相关代码添加到我的 'tableView(tableView:NSTableView,objectValueForTableColumn tableColumn:NSTableColumn?,row:Int) - > AnyObject? {}' – Wolfstar

+0

很高兴帮助:) – iRiziya