views:

638

answers:

2

Hello, I have i problem with tableView selected row. When i select the row it selected, but when i back in the previous navigation controller the row deselected. Can i do that iPhone remember what row was selected and when i push controller it remember selected row and can it be blue?

A: 

I suggest maintaining state in the iPhone by saving the selected row index to a file (info.plist? properties.plist). Then when you return to that view, get the selected row back from the file and change the selected row index.

Kyle
How can i save selected row index in a file?
app
http://stackoverflow.com/questions/905542/how-to-write-data-in-plistalternatively if you are new to iPhone development look into Titanium Appcelerator, which makes iPhone development much easier by allowing you to develop iPhone apps in Javascript.
Kyle
Your app should never write to Info.plist. A better place to store application state (as opposed to user data) is in the defaults system (see the NSUserDefaults Class Reference).http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
jlehr
+1  A: 

The way objects ordinarily 'remember' things is by storing them in instance variables. So the simple way to keep track of the selected row would be add an instance variable and property of type NSIndexPath * to the list controller. Then you could use the property to store the indexPath passed to the table view controller's -tableView:didSelectRowAtIndexPath: method.

Then your list controller can override the inherited -viewWillAppear: method to send a message to the table view to re-select the row, as follows:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Good idea to reload the table view's cells
    // in case the underlying data was changed by
    // another view controller.
    [[self tableView] reloadData];

    // Assuming you added a selectedIndexPath property
    // to your list controller...
    //
    if ([self selectedIndexPath] != nil)
    {
        // Select the row. Note that you need to be sure
        // that the table view is scrolled so that the
        // selected row is visible.
        [[self tableView] selectRowAtIndexPath:[self selectedIndexPath]
                                      animated:NO
                                scrollPosition:UITableViewScrollPositionMiddle];
        // Get rid of the saved selection
        [self setSelectedIndexPath:nil];
    }
}
jlehr