Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
950 views
in Technique[技术] by (71.8m points)

iphone - Console error: UICollectionView must be initialized with a non-nil layout parameter

I'm new to UICollectionView and I'm following a tutorial that I found on Youtube, but i'm stuck on an error I can't figure out.

When I run the app with this code:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return 1;

    }

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return [self.array count];

    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        CollectionCell *aCell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

        aCell.title.text = self.array[indexPath.row];

        return aCell;

    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

    }

And in the .h:

@property (strong, nonatomic) NSArray *array;

In the console I receive the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

Im NOT using storyboard, and custom maked the CollectionView what you can see here:

image

Does anyone have any ideas why i'm getting this error? Everything is welcome!

edit:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setItemSize:CGSizeMake(60, 60)];
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];

    [self.collectionView setCollectionViewLayout:flow];

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it is an error while registering the uicollectionviewcell view class. To resolve put the below line in your code:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...