0

我有一个 NSTableView 来管理我在应用程序左侧的导航。根据应用程序首次加载的日期,它应该自动选择那一天。

我的问题是如何在视图加载时以编程方式选择一行?

例如; 应选择星期一第 0 行。应选择星期五第 4 行

4

2 回答 2

1

您可以在选择行时进行检查:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if DayOfWeek() == indexPath.row{
        cell.backgroundColor = UIColor.grayColor()
    }
}

或者,当您编写表格时,您可以:

func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    if DayOfWeek() == indexPath.row{
        cell.backgroundColor = UIColor.grayColor()
    }
}

更新
当应用程序启动时,您应该viewDidLoad检查它是星期几并更新数据,以便您始终在应用程序启动时填充主视图。

And then when a new cell is selected you can do the following:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // indexPath.row is the row that the user has selected
    // use this value to update the data in your application
}
于 2016-03-14T05:13:52.347 回答
-2

这是 NSTableView 而不是 UITableView,为什么要投票?如果你想 pre_select 行,你需要继承 NSTableRowView 覆盖一些方法。

于 2017-03-21T08:24:11.967 回答