Reputation: 9596
The title basically says it all. I'm trying to create a collection view with a UICollectionViewController
that is defined in a Xib file. My UICollectionViewController
uses a custom base class and when I try to access collectionView
in its viewDidLoad()
, collectionView
is nil
. Code is very straightforward.
Can somebody tell me why collectionView is nil
when the UICollectionViewController
is loaded from a Xib and how to fix this? When I instantiate the UICollectionViewController
programmatically everything works but I'd prefer to create the UICollectionViewController
in a Xib file.
UPDATE:
Upvotes: 1
Views: 1740
Reputation: 196
Check your Xib delegate setting and code, follow these:
regist cell:
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:Cell_Identifier];
And see API document and demo directly: http://developer.apple.com/library/etc/redirect/xcode/ios/889e4b/samplecode/CollectionView-Simple/Introduction/Intro.html
The new screenshot and code.
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets top = {10,25,15,25};
return top;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
CGSize size = {25,25};
return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
CGSize size = {25,25};
return size;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(70, 70);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 30;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 30;
}
Upvotes: 2