Selecting initial row in TableView - error?
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:
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!
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!
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.
Insert a -reloadData call to the tableView before you attempt to select the row. Assuming there is data in the dataSource.
Cool, that works. Thanks a lot!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| SQLiteBooks example and initial database | Toontingy | 3 | 3,955 |
Jul 16, 2009 02:56 PM Last Post: Toontingy |
|
| A few initial questions about my first game (2D) | marcosscriven | 2 | 2,889 |
Jul 6, 2009 06:15 AM Last Post: marcosscriven |
|

