Skip to content

Commit

Permalink
Added BUG TableView State Restore
Browse files Browse the repository at this point in the history
+ Sample project to reproduce bug in state restoration code when
  a table view is embedded in a navigation controller.
  • Loading branch information
kharrison committed Apr 7, 2013
1 parent a3c2712 commit 5e6fa68
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BUG TableView State Restore/README
@@ -0,0 +1,29 @@
=======================================================================
Restore - State Preservation and Restoration of Table View

Version 1.0 07 April 2013 Initial version.
=======================================================================

This is a example project to reproduce a bug in the iOS 6 state preservation
and restoration code. The state of a table view is not restored when the table
is embedded in a navigation controller.

The project contains two storyboards to demonstrate state restoration of a table
view both when it is the root view and when it is embedded in a navigation
controller:

If the Target settings are used to set the Main Storyboad to NavStoryboard the
user inteface consists of UITableView embedded in a Navigation Controller. In
this use case the table view state is not restored.

If the Target settings are used to set the Main Storyboard to MainStoryboard the
user interface consists of a single UITableView which does have state restored
as expected.

For further details see the following blog post:

http://useyourloaf.com/blog/2013/04/07/bug-table-view-state-not-restored-when-embedded-in-navigation-controller.html

**************************************************************************
*** This bug has been tested and reproduced for iOS 6.0 and iOS 6.1.3. ***
**************************************************************************
13 changes: 13 additions & 0 deletions BUG TableView State Restore/UYLTableViewController.h
@@ -0,0 +1,13 @@
//
// UYLTableViewController.h
// restore
//
// Created by Keith Harrison on 17/03/2013.
// Copyright (c) 2013 Keith Harrison. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UYLTableViewController : UITableViewController

@end
92 changes: 92 additions & 0 deletions BUG TableView State Restore/UYLTableViewController.m
@@ -0,0 +1,92 @@
//
// UYLTableViewController.m
// restore
//
// Created by Keith Harrison on 17/03/2013.
// Copyright (c) 2013 Keith Harrison. All rights reserved.
//

#import "UYLTableViewController.h"

@interface UYLTableViewController () <UIDataSourceModelAssociation>

@end

@implementation UYLTableViewController

#pragma mark -
#pragma mark === UITableViewDataSource Delegate
#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1000;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Cell #%d",indexPath.row];
return cell;
}

#pragma mark -
#pragma mark === State Preservation ===
#pragma mark -

// The following two methods are the suggested workaround from Apple to
// forse the table view state to be restored when embedded in a
// navigation controller. The key is to force the table view to
// reload data when the state has been restored.

//- (void) encodeRestorableStateWithCoder:(NSCoder *)coder
//{
// // Save anything relevant for our role as the TableView's DataSource
// [super encodeRestorableStateWithCoder:coder];
//}
//
//- (void) decodeRestorableStateWithCoder:(NSCoder *)coder
//{
// [super decodeRestorableStateWithCoder:coder];
// // Restore whatever we need as the TableView's DataSource, and then...
// [self.tableView reloadData];
//}


// The following two methods are not strictly necessary in this simple example
// but you would normally want to implement any time the data displayed in the
// table view can change between saving and restoring the view.
//
// In this case we can also use this methods to force a reload of the table
// view data when restoring state. This is a workaround for a bug that causes
// the table view state to not be restored when embedded in a navigation controller.

- (NSString *)modelIdentifierForElementAtIndexPath:(NSIndexPath *)indexPath inView:(UIView *)view
{
NSString *identifier = nil;
if (indexPath && view)
{
identifier = [NSString stringWithFormat:@"%d",indexPath.row];
}
return identifier;
}

- (NSIndexPath *)indexPathForElementWithModelIdentifier:(NSString *)identifier inView:(UIView *)view
{
NSIndexPath *indexPath = nil;
if (identifier && view)
{
NSInteger row = [identifier integerValue];
indexPath = [NSIndexPath indexPathForRow:row inSection:0];
}

// Force a reload when table view is embedded in nav controller
// or scroll position is not restored. Uncomment following line
// to workaround bug.
// [self.tableView reloadData];

return indexPath;
}

@end

0 comments on commit 5e6fa68

Please sign in to comment.