Search code examples
iosxcodeipaduisplitviewcontrolleruicollectionview

Collection view controller in split view does not update


I created a project using the master detail application template. The project is targeted for both iphone and iPad.

My Master View contains a table view controller that is populated from DB data. No problems so far.

In the detail view I replaced the default view controller with a Collection view controller. I want for every row in the master table view to create a number of cells in the collection view.

Now in the storyboard, the iphone version has a segue between the table & collection (master/detail) controllers and everything works fine.

MasterViewController.m    
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Inbound *current = inbounds[indexPath.row];
        [[segue destinationViewController] setDetailItem:current];
    }
}

My custom object "Inbound" is being passed from the master to the detail view. When this occurs, the detail/collection view controller updates the cells.

DetailViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   //Go round the DB and update the cells
}

My problem is in the Split view of the iPad version. There is a relationship instead of a segue between master and detail view, in the storyboard. The only code that is executed when I select a table row is this:

MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        Inbound *current = inbounds[indexPath.row];
        self.detailCollectionViewController.detailItem = current;

        NSLog(@"%@",@"table cell clicked");
    }
}

I can see that my custom "Inbound" object is being properly passed to the detail view controller. But for some reason, the collection view does not update.

Any ideas?


Solution

  • On the iPhone version the new view is being pushed onto the screen, so one of your methods will be initialising the view based on the detailItem.

    In the iPad the detail view is already on the the screen. You need to ensure that your setter function for detail view will reload the data for the collection view - I'll bet you're not doing that at the moment...

    In other words, you need something like this (assuming ARC)

    - (void)setDetailItem:(Inbound *)detailItem
    {
        _detailItem = detailItem;
        [_collectionView reloadData];
    }
    

    Tim