0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Eli
  • 668
  • 2
  • 13
  • 37

2 Answers2

1

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
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • thank you, thins works for the interface but it doest actually set the row to selected, i use what row is selected to vary the data on the main window. This method of doing things makes the selected row -1 (aka no row selected) any other ideas? :) – Eli Mar 14 '16 at 05:21
  • If I understand you right, if it´s Monday then you want to show some specific data on the main window? Tuesday some other content? – Rashwan L Mar 14 '16 at 05:23
  • yip thats right! When a new cell is selected in queries core data and then updates another NSTableView – Eli Mar 14 '16 at 08:18
-2

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

Meonardo
  • 182
  • 13