0
votes

I have a NSTableView that manages my navigation on the left hand side of my application. Depending on what day it is when the app first loads it should auto select that day.

My question is how do I select a row programatically when the view loads?

Eg; Monday row 0 should be selected Eg; Friday row 4 should be selected

2

2 Answers

1
votes

You could make a check when the row is selected:

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

Or when you write the table you could:

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

Update
When the app is started you should in your viewDidLoad check what day of the week it is and update the data, so that you always populate the main view when the app is started.

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
}
-2
votes

This is NSTableView NOT a UITableView, why get upvoted? If you want to pre_select rows you need to subclass NSTableRowView override some methods.