Question

Goal:

Set up two collectionViews with different styles. One grid style and other single file style. These collectionViews will be toggleable giving users shopping within the ability to view the items for sale in which ever style (grid/single file) they prefer.

I have an existing collectionView with a custom cell set up in interface builder and it's working fine. I've tried to find info on how to add a second one via interface builder but have had no luck in finding any.

What I've done: I've created the second collectionView programatically in my viewDidLoad method. I have an instance variable named _collectionView2.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView2 = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [_collectionView2 setDataSource:self];
    [_collectionView2 setDelegate:self];

    [_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [_collectionView2 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:_collectionView2];
    [_collectionView2 setHidden:YES];

I've modified delegate and datasource methods to make them aware about the new collectionView:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collectionView) {
      NSArray *people = [_thisController objects];
      return [people count];
    } else {

        return 20;
    }
}

.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{      
    if (collectionView == _collectionView) {
         static NSString *CellIdentifier = @"Cell";
        VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];

        [[cell activityIndicator] startAnimating];

        PFFile *userImageFile = [object valueForKey:@"image"];
        [[cell imageView] setFile: userImageFile];
        [[cell imageView] loadInBackground];

        [[cell activityIndicator] stopAnimating];

        [[cell title] setText:[object valueForKey:@"title"]];
        [[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
        return cell;
    } else  {
         static NSString *CellIdentifier = @"Cell";

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

        cell.backgroundColor = [UIColor greenColor];

        return cell;
    }

    //_addToFavouritesButton = [cell addFavouriteButton];

    [_addToFavouritesButton addTarget:_thisController action:@selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

 }

.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collectionView2) {

      return CGSizeMake(50, 50);

    } else {
        return CGSizeMake(140, 272);
    }
}

Then I have a method that responds to the UISegmentControl being switched from grid style display to single file display:

- (void)displayTypeSegmentSelected
{
    _selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];

    if (_selectedDisplayTypeIndex == 0) {
        NSLog(@"Single file item view selected");

        [_collectionView setHidden:YES];

        [_collectionView2 setHidden:NO];
    } else {
        NSLog(@"Grid style view selected");
        [_collectionView setHidden:NO];
        [_collectionView2 setHidden:YES];
    }
}

CollectionView2 is hidden initially then unhidden when the segment control is used. It looks like it's working because when I toggle a red background shows and I did set a red background when I created the collectionView but green cells aren't showing at all.

I can't seem to see where I've gone wrong. I'd like to get this working so I can replace the cell with my custom cell. The only difference between grid display and single file display is that single file display will be an enlarge cell that takes up the width of the view. All properties etc will be the exact same meaning I can use my current custom cell.

Why aren't my cells showing? I've followed a clear tutorial on creating a collectionView programmatically but still no cells are showing.

Update:

I put a log message in the if statement that checks what collectionView is present and it is only ever trigger for my _collectionView and not _collectionView2.

Help is appreciated.

Regards

Was it helpful?

Solution 2

Reloading the table data in the method my UISegmentedControl triggers got the cells to show up.

OTHER TIPS

Not sure this will fix your issue, but when I have 2 collection views in one controller, I use their tag's to determine which one I am dealing with in a given function, like cellForItemAtIndexPath.

I also have always used different cell identifiers for the two cells and usually different subclasses of uicollectionviewcell.

Where do you create the first collection view and set its delegate?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top