Selecting initial row in TableView - error?

Apprentice
Posts: 9
Joined: 2008.10
Post: #1
Hi, I'm having a problem making a tableview with a row initially selected, I hope someone can help?

I've set up a tableview using a similar pattern to the sample code "SimpleTableView" in TableViewSuite. One change I did make is to modify willSelectRowAtIndexPath so that row selection is possible.

The tableview is working correctly and displays my data etc.

Then I added code so that a particular row is initially selected when the tableview appears:

Code:
// Create tableview
    pSampleTableController = [[SampleTableController alloc] initWithStyle:UITableViewStylePlain];
    [self addSubview:pSampleTableController.tableView];

// Select initial row    
    NSIndexPath* initialSample = [NSIndexPath indexPathForRow:initialRow inSection:0];
    [pSampleTableController.tableView selectRowAtIndexPath:initialSample
          animated:NO scrollPosition:UITableViewScrollPositionMiddle];


This code causes an exception at the selectRowAtIndexPath call.
When I move the "Select Initial Row" code to be called when I press a button later on, it works fine. It just causes an error when I try to call it when creating the TableView.

I'm assuming that the tableview needs a bit of time to be setup correctly, so I cant call selectRow right away. I tried putting the selection into SampleTableController::viewDidLoad, but that doesnt work either.

Is there a function that the API calls when the tableview is ready? Or is my only option to create a timer that makes the initial selection after a bit of time has passed? Or am I just doing something wrong?

Thanks!
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
There is no data loaded into the UITableView instance. You can't select a row that doesn't exist, and the exception that you get tells you that: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (0) beyond bounds (0) for section (0).'

Insert a -reloadData call to the tableView before you attempt to select the row. Assuming there is data in the dataSource.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.10
Post: #3
Cool, that works. Thanks a lot! Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SQLiteBooks example and initial database Toontingy 3 4,577 Jul 16, 2009 02:56 PM
Last Post: Toontingy
  A few initial questions about my first game (2D) marcosscriven 2 3,294 Jul 6, 2009 06:15 AM
Last Post: marcosscriven