Wiele UICollectionView w jednym kontrolerze

Mam widok skonfigurowany z dwoma UICollectionViews. Każdy z tych widoków ma tablicę wspierającą go w różnych rozmiarach. Kolekcja1 jest wspierana przez tablicę1, a kolekcja2 jest wspierana przez tablicę2. Problem polega na tym, że kiedykolwiek zwrócona liczba dla kolekcji1 z numberOfItemsInSection jest stosowana do obu widoków kolekcji.

Na przykład, jeśli array1 ma rozmiar 4, a array2 ma rozmiar 5, obie kolekcje pokażą 4 elementy. Jeśli tablica1 ma rozmiar 5, a tablica2 ma rozmiar 4, kiedy przewijam kolekcję2 aż do wywołania cellForItemAtIndexPath z itemIndex 5 dla kolekcji2 i otrzymam NSRangeException.

Jak mogę sprawić, aby każda kolekcja korzystała z własnego rozmiaru?

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    if(view == self.colleciton1){
        return self.array1.count;
    } else if (view == self.collection2){
        return self.array2.count;
    }

    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    if(cv == self.collection1){
        CharacterCell *cell = [cv dequeueReusableCellWithReuseIdentifier:FIRST_CELL_IDENTIFIER forIndexPath:indexPath];
        cell.label.text = self.array1[indexPath.item];
        return cell;
    } else if (cv == self.collection2){
        EpisodeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:SECOND_CELL_IDENTIFIER forIndexPath:indexPath];
        cell.label.text = self.array2[indexPath.item];
        return cell;
    }

    return nil;
}

Dodałem repozytorium git z projektem ilustrującym problem.

[email protected]: civatrix / MultipleCollectionViews.git

questionAnswers(3)

yourAnswerToTheQuestion