Reputation: 5976
I have two UICollectionViews
in my UIViewController
.
Let's say they are A and B, A and B are positioned at the same place in UIViewcontroller in the nib file, they have exact same sizes.
Now I set
A.delegate = self;
A.dataSource = self;
B.delegate = self;
B.dataSource = self;
[A registerNib:nib forCellWithReuseIdentifier:CELL_A];
[B registerNib:nib forCellWithReuseIdentifier:CELL_B];
in ViewDidLoad
then in CellForItemAtIndexPath
,
I use a switch statement to show different cells.
then I use a segment control to control displaying a correct UICollectionViewcontroller
on my selected segment.
In segment control value changed code. I use:
- (IBAction)sectionSelected:(id)sender {
if (choosing A) {
B.hidden = YES;
A.hidden = NO;
A.backgroundColor = [UIColor blackColor];
[self getAData];
[A reloadData];
}
else if (choosing B) {
A.hidden = YES;
B.hidden = NO;
B.backgroundColor = [UIColor orangeColor];
[self getBData];
[B reloadData];
}
}
A is displayed correctly when I set default selected segment as A. but when I switch to B, I can see B is load but the data is not loaded to B so I couldn't see anything except for the orange background. I put the NSLog in CellForItemAtIndexPath
and find that the method is not being called after reloadData
.
Here is some logging I get for A.collectionViewLayout.collectionViewContentSize;
and B.collectionViewLayout.collectionViewContentSize;
content size of B = 768.000000, 24.000000
content size of A = 768.000000, 880.000000
When I set default selected segment as B, B is displayed correctly. I switch back to A which is also displayed correctly. However when I switch back to B again it shows an empty orange screen. Any idea what have gone wrong with my code?
Upvotes: 1
Views: 3396
Reputation: 234
Is your getAData/getBData an asynchronous call? You may be hitting a race condition where the data isn't ready, so when you call reloadData the number of cells the collection view is creating is 0. This would also explain the small height in the content size.
Upvotes: 0
Reputation: 1578
Probably you have lost the data of items in your array object, which you using for reloading/creating the collection view cells. Double check on array/data.
Upvotes: 1
Reputation: 405
From this answer
Change
[A reloadData]/ [B reloadData]
to:
[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
[self.collectionView reloadData];
Upvotes: 2