Reputation: 468
I'm trying to do something with an UICollectionView. I want them centered in the middle, but therefor i need to create something like this:
(Array consists of 5 items)
X X <- 2 items in section 1
X X <- 2 items in section 2
X <- 1 item in section 3
But the problem is that the items doesn't show up right? If i would have 3 items. The items would be like, In section 1, item 1 and 2. and then in section 2 would be item 1 again, instead if item 3 of my array.
This is my code:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
float uitkomst = ceilf((float)[arrayDobbel count] / 2) - 1; // Array starts at 0 so need to do - 1
if (uitkomst == section) {
return 1; // Last cell
}
return 2; //Always 2 cells to each other, not the last section
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
float uitkomst = ceilf((float)[arrayDobbel count] / 2); //Calculate how much sections. Always 2 items / section
return (int)uitkomst;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
detailDice *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dobbelsteenGame" forIndexPath:indexPath];
cell.layer.cornerRadius = 25;
cell.layer.backgroundColor = [[UIColor colorWithRed:0.188 green:0.341 blue:0.612 alpha:1.0] CGColor];
[[cell detaildiceLabel] setText: [NSString stringWithFormat:@"%@", [[arrayDobbel objectAtIndex:indexPath.row] returnRandomOptie]]];
return cell;
}
Could anyone help me with the display of the right texts in the right cells?
Kind Regards
Upvotes: 0
Views: 1389
Reputation: 5182
In cellForItemAtIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
....
//since there is 2 items in each section
int index = 2*indexPath.section + indexPath.row;
[[cell detaildiceLabel] setText: [NSString stringWithFormat:@"%@", [[arrayDobbel objectAtIndex:index] returnRandomOptie]]];
return cell;
}
Upvotes: 2