Table view not reloading - cellForRowAtIndexPath not being called

Discussion in 'iOS Programming' started by mariusw, Jun 9, 2009.

  1. mariusw macrumors newbie

    Joined:
    Jun 9, 2009
    #1
    Table view not reloading - cellForRowAtIndexPath not being called

    My application is based on the SQLiteBooks tutorial from Apple. Instead of MasterViewController, I'm using CustomerViewController, i.e. books are now customers. All customer operations work as expected.

    But I've added a new level below the customer level: projects. I've modified the customer view so that it now displays a blue accessorydetaildisclosure button. Clicking this button shows the customer details. And I've added the delegate method willSelectRowAtIndexPath. What this does is to first call a method on the appdelegate (getProjectsForCustomer) that queries the database for the selected customer's projects. Next, a ProjectViewController instance is created and then this is pushed onto the navigation controller.

    The first time this is done, everything happens as expected:
    the viewDidLoad method is first called, which just creates an add button on the nav bar.
    Then the viewWillAppear method is called, which first gets the projects from the app delegate, and then calls reloadData on the tableview.
    Then the cellForRowAtIndexPath is called repeatedly.
    All as expected.

    However; when I navigate back to the customer view, and then open another customer, only viewWillAppear is called on the project view (of course, the willSelectRowAtIndexPath is called on the customer view). The cellForRowAtIndexPath is not called, the result being that the same projects are being listed again, instead of the view redrawing itself based on the updated projects list.

    Help is greatly appreciated!

    // Marius
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You sure the reloadData is being called for the correct tableView? Perhaps you can include some code for us to examine.
     
  3. mariusw thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #3
    It seems the tableView property is not set.

    In my CustomerViewController (which is a UITableViewController) I create a ProjectViewController in the willSelectRowAtIndexPath method:

    Code:
    - (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	// Select customer and navigate to project list
    	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        Customer *cust = [appDelegate.customers objectAtIndex:indexPath.row];
    	
    	// Get customer's projects from database
    	[appDelegate getProjectsForCustomer:cust];
    	
    	ProjectViewController *controller = self.projectViewController;
    	controller.customer = cust;
    	
    	[self.navigationController pushViewController:controller animated:YES];
    	[controller setEditing:NO animated:NO];
    	return nil;
    	
    }
    The projectViewController method used in the CustomerViewController class:

    Code:
    - (ProjectViewController *)projectViewController {
        // Instantiate the project view controller if necessary.
        if (projectViewController == nil) {
            //projectViewController = [[ProjectViewController alloc] initWithNibName:@"ProjectsView" bundle:nil];
    		projectViewController = [[ProjectViewController alloc] initWithStyle:UITableViewStylePlain];
    		projectViewController.title = @"Projects";
        }
        return projectViewController;
    }
    I've placed a breakpoint in the viewWillAppear method on the ProjectViewController, on the [self.tableView reloadData]; line.

    Before I execute this, I check the self argument list and see that the tableView variable is not set (value 0x0).

    Still, the first time this is executed, the cellForRowAtIndexPath is called, returning the correct cells. In the simulator I then return to the customer list and then tap a different customer. Upon reaching the viewWillAppear breakpoint again I see that the tableView property (in self) is once again not set, and this time the cellForRowAtIndexPath is not called. The project list from the previous customer is displayed directly.

    I get a feeling there is a "ghost tableview" hiding somewhere here. And I presume that the reason cellForRowAtIndexPath is not called is that I try to reload a tableview that is not set, while the correct tableview (hiding somewhere) is not reloaded.

    If you want to look at the entire code in the CustomerViewController, ProjectViewController and AppDelegate, I've put them here:
    http://seraph.bluebricks.no/files/code/

    Thanks again!

    Marius
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    No. You define it as an instance variable but you never set it in the code. That's why it has no value.
     
  5. mariusw thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #5
    True, but that is also the case with CustomerViewController?

    I thought that the OS would set it in the background during init:
    projectViewController = [[ProjectViewController alloc] initWithStyle:UITableViewStylePlain];

    How, then, should I set it?
     

Share This Page