我正在尝试使用UICollectionView执行某些操作。我希望他们在中间居中,但因此我需要创建这样的东西:
(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
但问题是这些物品没有出现吗?如果我有3个项目。这些项目就像在第1部分第1项和第2项中那样,然后在第2部分再次是第1项,而不是我的数组的第3项。
这是我的代码:
- (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;
}
有人可以帮助我在正确的单元格中显示正确的文本吗?
亲切的问候
答案 0 :(得分:2)
在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;
}