开发者

How can see always selected tableView row? (iPhone)

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?


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];
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜