首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在uicollectionView中,每隔4个块就有一个背景色。

在uicollectionView中,每隔4个块就有一个背景色。
EN

Stack Overflow用户
提问于 2014-08-01 08:22:10
回答 2查看 227关注 0票数 0

我有一个collectionView和一个cells列表。每个cell都有一个backgroundColor。所以应该是这样。

  • 细胞1->红色
  • 细胞2->绿色
  • 细胞3->橙色
  • 细胞4->黄色
  • 细胞5->红色
  • 细胞6->绿色
  • 细胞7->橙色
  • 细胞8->黄色
  • 细胞9->红色
  • 细胞10 ->绿色
  • 细胞11 ->橙色
  • 细胞12 ->黄色

现在我就是这样做的。我在我的ViewController中声明了一个ViewController,在我的cellForItemAtIndexPath中我这样做了。

代码语言:javascript
复制
- (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;
}

如果你慢慢滚动的话,这是可行的。但是,如果你正在快速滚动,颜色就会变得混乱。有人能帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2014-08-01 08:26:50

您需要依赖indexPath.row,而不是使用手动单元格计数。它不能工作,因为您的单元格在滚动UICollectionView时会被重用。

假设您的UICollectionView中有一个部分,您可以使用类似的方法来确定每个单元格的颜色:

代码语言:javascript
复制
switch (indexPath.row % 4) {
        case 0:
            // yellow
            break;
        case 1:
            // orange
            break;
        case 2:
            // green
            break;
        case 3:
            // red
            break;
    }

我还没有测试过这个逻辑,但是这个一般的方法应该有效。

票数 1
EN

Stack Overflow用户

发布于 2014-08-01 08:28:18

问题是:

代码语言:javascript
复制
if(countBackground>4){
    countBackground = 0;
}

您将countBackground设置为0,然后不要在单元格中增加。解决办法是使用countBackground = 1

一个更好的解决方案是使用模数运算。使用:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25075873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档