Question

I'm trying to use RFQuiltLayout with my UICollectionView.

I have the CollectionView working fine with the standard UICollectionViewFlowLayout but it's just a grid. I want my photos to layout nicely as shown here.

I'm having some trouble understanding what's required to use a custom layout with my UICollectionView.

I'm doing everything programatically. I'm not using any Interface Builder/NIBs/Storyboards.

Getting the UICollectionView to layout with the standard FlowLayout was easy, but when I tried to change to the RFQuiltLayout I ran into the following error:

'UICollectionView must be initialized with a non-nil layout parameter'

This seems to be a common error, but none of the suggestions/answers on the other questions helped me resolve it.

So here's the relevant portions of my code:

.h

@interface PFRootViewController : UIViewController <OFFlickrAPIRequestDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;

.m

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

//Used to use the above standard FlowLayout, but now trying to move to the below RFQuiltLayout

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 40);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];



self.collectionView.delegate = self;
self.collectionView.dataSource = self;

[self.collectionView registerClass:[PFUICollectionViewCell class] forCellWithReuseIdentifier:@"FlickrCell"];


//Only Layout delegate method i've implemented:
- (CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0)
        return CGSizeMake(80, 40);

    return CGSizeMake(40, 80);
}

I really don't know what else I've got to do to get this working. If I simply remove the RFQuiltLayout references and uncomment my UICollectionViewFlowLayout alloc init everything works, albeit with the standard FlowLayout.

So if anyone can help point me in the right direction that would be great, this should be simple but it's been a real pain to try and get working. - UICollectionViews aren't as simple as UITableViews.

I spent a good portion of the day yesterday trying to get various custom layouts for my UICollectionView working (including RFQuiltLayout) without any success.

Any help is greatly appreciated,

Regards, John

Was it helpful?

Solution

RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];

Here you're asking the collection view for a layout object, two lines before you initialise the collection view. Compare with the line you commented out:

//UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

You need to create a new layout object:

RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];

Then assign it to your view as present.

Looking at the README of the repository, the problem is that you are copying code from a viewDidLoad method, which assumes that the collection view and its layout have been initialised from a storyboard. If you're creating your view in code, you need to create the layout as well.

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