我有一个collectionView和一个cells列表。每个cell都有一个backgroundColor。所以应该是这样。
现在我就是这样做的。我在我的ViewController中声明了一个ViewController,在我的cellForItemAtIndexPath中我这样做了。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// MagazineCollectionViewCell *cell = (MagazineCollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"MagazineCollectionViewCell " forIndexPath:indexPath];
ReisCategorieCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ReisCategorieCell" forIndexPath:indexPath];
cell.delegationListener = self;
Reiscategorie *category = [arrCategories objectAtIndex:indexPath.row];
cell.reisCategorie = category;
cell.lblTitle.font = [UIFont fontWithName:@"Dosis-Medium" size:35];
cell.lblDescription.font = [UIFont fontWithName:@"Dosis-Medium" size:15];
[cell.btnAanbod.titleLabel setFont:[UIFont fontWithName:@"Dosis-Medium" size:23]];
cell.lblTitle.textColor = [UIColor whiteColor];
cell.lblDescription.textColor = [UIColor whiteColor];
[cell.btnAanbod.titleLabel setTextColor:[UIColor whiteColor]];
cell.btnAanbod.titleLabel.textColor = [UIColor whiteColor];
countBackground++;
if(countBackground>4){
countBackground = 0;
}
if (countBackground == 1) {
cell.contentView.backgroundColor = [UIColor aslRed];
cell.btnAanbod.backgroundColor = [UIColor aslBrown];
}else if (countBackground == 2){
cell.contentView.backgroundColor = [UIColor aslBrown];
cell.btnAanbod.backgroundColor = [UIColor aslGray2];
}else if (countBackground == 3){
cell.contentView.backgroundColor = [UIColor aslGray];
cell.btnAanbod.backgroundColor = [UIColor aslBrown];
}else if (countBackground == 4){
cell.contentView.backgroundColor = [UIColor aslGray2];
cell.btnAanbod.backgroundColor = [UIColor aslBrown];
}
if([category.cat_name isEqualToString:@"Lapland"]){
cell.btnLapland.hidden = NO;
}else{
cell.btnLapland.hidden = YES;
}
[cell.btnAanbod setTitle:NSLocalizedString(@"btnAanbod", nil) forState:UIControlStateNormal];
cell.lblTitle.text = category.cat_name;
cell.lblDescription.text = category.cat_description;
return cell;
}如果你慢慢滚动的话,这是可行的。但是,如果你正在快速滚动,颜色就会变得混乱。有人能帮我吗?
发布于 2014-08-01 08:26:50
您需要依赖indexPath.row,而不是使用手动单元格计数。它不能工作,因为您的单元格在滚动UICollectionView时会被重用。
假设您的UICollectionView中有一个部分,您可以使用类似的方法来确定每个单元格的颜色:
switch (indexPath.row % 4) {
case 0:
// yellow
break;
case 1:
// orange
break;
case 2:
// green
break;
case 3:
// red
break;
}我还没有测试过这个逻辑,但是这个一般的方法应该有效。
发布于 2014-08-01 08:28:18
问题是:
if(countBackground>4){
countBackground = 0;
}您将countBackground设置为0,然后不要在单元格中增加。解决办法是使用countBackground = 1
一个更好的解决方案是使用模数运算。使用:
countBackground = indexPath.row % 4
if (countBackground == 0) {
//you are in row 0, 4, 8, ... -> Red color
} else if (countBackground == 1) {
//you are in row 1, 5, 9, ... -> Green color
} else if (countBackground == 2) {
//you are in row 2, 6, 10, ... -> Orange color
} else if (countBackground == 3) {
//you are in row 3, 7, 11, ... -> Yellow color
}https://stackoverflow.com/questions/25075873
复制相似问题