2015-08-13 66 views
1

如何获得行的前面选择的指数NSTableViewNSTableView的旧选择

方法

func tableViewSelectionIsChanging(notification: NSNotification) { 
    let index = (notification.object as! NSTableView).selectedRow 
    println(index) 
} 

func tableViewSelectionDidChange(notification: NSNotification) 
    let index = (notification.object as! NSTableView).selectedRow 
    println(index) 
} 

两种方法打印相同的值。如何获得因选择而发生变化的索引/行?


或者简单来说,如何让我喜欢

println("\(Selection changed from \(tableView.oldSelectedIndex) to \(tableView.newSelectionIndex)") 

回答

2

得到它选择了一个小区之前,该函数func tableView(tableView: NSTableView, shouldSelectRow row: Int)是呼吁delegate。在这里,您可以看到当前选定的行和建议的选择。

func tableView(tableView: NSTableView, shouldSelectRow row: Int) -> Bool { 

    // This is the next index 
    NSLog("Next index: \(row)") 

    // Make sure that a row is currently selected! 
    guard self.tableView.selectedRowIndexes.count > 0 else { 
     return true 
    } 

    // Get the currently selected row. 
    let index = self.tableView.selectedRow 
    NSLog("Previous index: \(index)") 

    return true 
} 

注意:这不正是回答你的问题,因为你已经要求新的选择后的变化发生,而这之前给它。但我希望这已经足够了。

+0

由于不调用tableView(tableView:NSTableView,shouldSelectRow row:Int)'当用户取消选择一行时,此技术将不起作用。相反,使用委托方法'tableView(_ tableView:NSTableView,selectionIndexesForProposedSelection proposedSelectionIndexes:IndexSet) - > IndexSet'。 –

0

一份声明中您可以通过

tableView.selectedRow 
+0

此答案不适用于多项选择,更重要的是,此答​​案在选择更改后返回选择,而不是BFORE。该问题具体要求在改变之前进行选择。 –