I am currently trying to implement a simple two-row UICollectionView. Because I already had this issue when using a more complex data source involving Core Data I made it just an NSArray containing some strings. Still the problem is the same.
- (NSArray *)collectionViewData
{
return @[@"zero",@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",@"ten",@"eleven",@"twelve",@"thirteen",@"fourteen",@"fifteen",@"sixteen", @"seventeen"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.collectionView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(160, 120);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 10;
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
self.collectionView.dataSource = self;
[self.collectionView registerClass:[BYCollectionViewCell class] forCellWithReuseIdentifier:@"CELL_ID"];
[self.view addSubview:self.collectionView];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.collectionViewData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL_ID" forIndexPath:indexPath];
cell.title = self.collectionViewData[indexPath.row];
return cell;
}
http://bytolution.com/scrnsht.png
As you can see, the cells are in the wrong order. When I set the minimumLineSpacing
to 0 they even change their position.
How can I implement UICollectionView correctly?