首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTableView老选择

NSTableView老选择
EN

Stack Overflow用户
提问于 2015-08-13 20:12:35
回答 2查看 521关注 0票数 1

如何在NSTableView中获得前面选定的行索引

方法

代码语言:javascript
复制
func tableViewSelectionIsChanging(notification: NSNotification) {
     let index = (notification.object as! NSTableView).selectedRow
     println(index)
}
代码语言:javascript
复制
func tableViewSelectionDidChange(notification: NSNotification)
    let index = (notification.object as! NSTableView).selectedRow
    println(index)
}

两种方法都打印相同的值。如何获得由于选择而发生更改的索引/行?

或者简单地说,我如何做一个像这样的语句?

代码语言:javascript
复制
println("\(Selection changed from \(tableView.oldSelectedIndex) to \(tableView.newSelectionIndex)")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-30 14:29:09

在选择单元格之前,在func tableView(tableView: NSTableView, shouldSelectRow row: Int)上调用函数delegate。在这里,您可以看到当前选定的行和建议的选择。

代码语言:javascript
复制
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
}

注:这并不能完全回答你的问题,因为你要求在新的选择发生后进行更改,而这只是在之前。不过,我希望这是足够的。

票数 3
EN

Stack Overflow用户

发布于 2015-08-16 03:30:12

你可以让它通过

代码语言:javascript
复制
tableView.selectedRow
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31997557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档