Reputation: 2777
I have a collectionView
with a list of cells
. Each cell
has a backgroundColor
. So it should be like this.
At the moment I'm doing it like this. I've declared a private int
in my ViewController
and in my cellForItemAtIndexPath
I do this.
- (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;
}
This works if you scroll slowly. But if you are scrolling to fast the colors are getting mixed up. So can anybody help me with this?
Upvotes: 0
Views: 231
Reputation: 994
The problem is:
if(countBackground>4){
countBackground = 0;
}
You set countBackground
to 0 and don't increment in the cell after that. A soulution would be to use countBackground = 1
A better solution would be using the Modulo operation. Use:
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
}
Upvotes: 0
Reputation: 1576
You need to rely on indexPath.row
instead of using manual cell counting. It doesn't work because your cells get reused while you scroll your UICollectionView
.
Assuming that you have one section in your UICollectionView
, you may use something like this to determine color for each cell:
switch (indexPath.row % 4) {
case 0:
// yellow
break;
case 1:
// orange
break;
case 2:
// green
break;
case 3:
// red
break;
}
I haven't tested this logic, but this general approach should work.
Upvotes: 1