BadmintonCat
BadmintonCat

Reputation: 9596

collectionView is nil in UICollectionViewController loaded from Xib

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:

enter image description here

Upvotes: 1

Views: 1740

Answers (2)

se7en
se7en

Reputation: 21

try it here.

The Example create collectionView with Xib

Upvotes: 0

Allen
Allen

Reputation: 196

Check your Xib delegate setting and code, follow these:

enter image description here

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.

enter image description here

enter image description here

-(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

Related Questions