views:

55

answers:

5

I have a DetailsViewController class and an ItemsViewController class. (Both derived from UITableViewController)

Selecting any of the items in the ItemsViewController brings up the DetailsViewController. In order to get it to show the new data on any but the first one, I currently have

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[self navigationItem] setTitle:title];
    [[self tableView] reloadData];
}

This works, however it feels like killing a fly with a sledgehammer. What is a better way to do this?

Thanks in advance,

Alan

A: 

If you know that only specific rows or sections will be changing, you can direct the bake view to reload only those rows or sections. Other than that, -reloadData is the way to go for most table views.

Jeff Kelley
A: 

I assume the items on the detail table changes depending on the selected item on the items table. So, yeah, this should be alright.

Other than that, you can check if the same item is selected the last time and not call reloadData during that case.

Altealice
A: 

Alan,

Your statement of "In order to get it to show the new data on any but the first one" concerns me - because it tells me that you likely have a single DetailsViewController instance.

In your first table view, ItemsViewController, you probably have a didSelectRowAtIndexPath: method that you're using to push the DetailsViewController onto the UINavigationController stack.

How I solve this issue is simply creating/destroying a new DetailsViewController every time my user taps between views. So, my didSelectRowAtIndexPath: often looks like:

- (void) didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
  NSInteger selectedRow = indexPath.row;

  // Create a new view controller
  DetailsViewController *tmpVC = [[DetailsViewController alloc] initWithNibName:@"foo" bundle:nil];

  // Tell our new view controller what data it should be using
  tmpVC.tableData = [self.someArrayOfData objectAtIndex:selectedRow];

  // Push view controller and release it
  [self.navigationController pushViewController:tmpVC animated:YES];
  [tmpVC release];
}

This example assumes that you have all the data necessary for both view controllers in your ItemsViewController - that may not be the case..?

Anyway, by doing it this way, your DetailsViewController automatically loads the data. When you tap "Back" to go back to ItemsViewController, the UINavigationController would release it, destroying it. Then, when the user taps a different cell, we run this code again, creating a brand-new controller with brand-new data - so of course when it displays, it will load the data automatically - it's never displayed before.

What it sounds like you may be doing in your code is retaining the DetailsViewController as a property of the ItemsViewController class and then reusing the object. This can also work as well if you're concerned about allocations (for example, if it is a very "heavy" allocation to make a DetailsViewController), but then I think the best place to call reloadData is not inside the class itself - but rather from the didSelectRowAtIndexPath: method of ItemsViewController.

The reason I promote the creation/destruction approach as opposed to the "flyweight pattern" approach is that it keeps your code more separate - the fewer linkages between view controllers, the better. Of course, ItemsViewController will always dependo on and know about DetailsViewController, but it shouldn't necessarily have to be the other way around - and if you add the reloadData call to viewWillAppear:animated:, you're implicitly adding a non-code dependency between the two. You know that when ItemsViewController is the "parent" in the navigation stack, that's the right behavior -- but what if you suddenly started reusing that view in other part of your app that doesn't require a reload? It's a performance hit for one, and moreover, it's the kind of hidden dependency that may end up in a nasty-to-trace bug someday. So, I'd keep Details stupid and make Items contain all the complexity, if it is indeed required to only have 1 DetailsViewController (as opposed to my first idea of recreating it each time).

phooze
A: 

I would propose the reloadData and setTitle to be in the viewDidLoad and in the setter - I assume you set a property in DetailsViewController that changes the datasource of the table. So viewDidLoad reloads and sets the title, if the property has been set, the setter reloads and sets the title if isViewLoaded and the new value is different than the old one.

- (void)setSmth:(SmthClass *)value {
     if (value == smth)  // if they are the same and SmthClass is immutable,
                         // otherwise use isEqual and [self.tableView reloadData]
                         // before returning...
         return;
     id pointer = smth;  // if it's a retain property
     smth = [value retain];
     [pointer release];      // release after retain just to be extra safe

     if ([self isViewLoaded]) { 
          [self.tableView reloadData];
          [self setTitle:title];
     }
}

- (void)viewDidLoad {
     if (smth) {
         [self.tableView reloadData]; // maybe redundant...
         [self setTitle:title];
     }
}

Or you can use Key-Value observing (NSKeyValueObserving protocol) to observe your property and reloadData on notification...

tsakoyan