tags:

views:

106

answers:

2

Hi, My problem seems quite simple but I have failed to find a solution here or elsewhere. I have a UITableView as a subclass in one of my UIViews. When the application finishes the last selected table cell is saved to NSUserDefaults and when the aplication restarts I want to set the seleced cell as it was before. However, this causes problems when I do it too early as the number of sections are unknown, ie the table hasn't loaded its data yet. So I decided to set it in the function numberOfRowsInSection, which works but I am sure is not the correct place to do this. Any help would be much appreciated.

Blockquote

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int iNofRows = 0; //Default

    // It is not great doing this here but.... if(bSetDefaultSelection == YES)
    {
    bSetDefaultSelection = NO; // Stop recursion
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0];
    [self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];

    }
    return iNofRows;
    }

+2  A: 

Hi Alan.

I think the place you're looking for is

- (void)viewDidAppear:(BOOL)animated;    
 // Called when the view has been fully transitioned onto the screen. Default does nothing

(See UIViewController for more information) I've just debugged my app and that method is called after the one you've mentioned:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

In your case you'd have to write something like:

- (void)viewDidAppear:(BOOL)animated{
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(NSUInteger)l last_sel_row inSection:(NSUInteger)0];
    [self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}

Cheers!

Lio
A: 

Hey thanks for the reply. I thought nobody answered so much appreciate the effort. The solution with '(void)viewDidAppear:(BOOL)animate' only works with UITableViewController but I am dealing with UITableView. But I decided to post a notification in my main controller that triggers the table to select and that works fine. Thanks again.

Alan Aherne