【问题标题】:NSTableView old selectionNSTableView 旧选择
【发布时间】:2016-01-30 22:29:09
【问题描述】:

如何获取NSTableView中之前选中的行索引

方法

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)")

【问题讨论】:

    标签: macos cocoa nstableview


    【解决方案1】:

    在选择单元格之前,函数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
    }
    

    注意:这并没有完全回答您的问题,因为您在新的选择发生后要求更改,而这只是之前给出的。尽管如此,我希望这已经足够了。

    【讨论】:

    • 当用户取消选择一行时,此技术将不起作用,因为未调用tableView(tableView: NSTableView, shouldSelectRow row: Int)。相反,使用委托方法tableView(_ tableView: NSTableView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet
    【解决方案2】:

    你可以得到它

    tableView.selectedRow
    

    【讨论】:

    • 此答案不适用于多项选择,更重要的是,此答​​案返回选择更改后的选择,而不是 BFORE。该问题专门要求在更改之前进行选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多