ios - objc 如何更改 UICollectionView 中第一行单元格的颜色?

标签 ios objective-c uicollectionview

我有一个包含两个部分的 UICollectionView。 我想更改每个部分中第一行单元格的 backgroundColor 属性。

我试过这个:

-(UICollectionViewCell *)collectionView:(UICollectionView *)aCollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 .... snip
    if (indexPath.row == 0) {
        cell.backgroundColor = [UIColor redColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
 .....  snip 

}

但这只会改变第 1 行第 1 列的颜色。

这很奇怪,但我找不到这样的问题。让我知道,我会删除这个问题。

更新:

节数..

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.dataArray.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];
    return [sectionArray count];
}

数据数组现在是垃圾数据,但看起来像这样:

for (int i=0; i<50; i++) {
        [firstSection addObject:[NSString stringWithFormat:@"Cell %d", i]];
        [secondSection addObject:[NSString stringWithFormat:@"item %d", i]];
    }

    self.dataArray = [[NSArray alloc] initWithObjects:firstSection, secondSection, nil];

最佳答案

为设置了部分条件的 cellForItemAtIndexPathelse 部分编写相同的代码段

关于ios - objc 如何更改 UICollectionView 中第一行单元格的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178011/

相关文章:

ios - NSDictionary 发布 Unicode 字符

iOS 在 main.m 中崩溃,如何查找错误?

ios - 如何使用 Swift 在 XCode 中制作带有动画的 LauchScreen?

ios - 不应该的不可变属性

ios - 您是否将 NSFetchRequest 放入 cellForRowAtIndexPath 中?

ios - 具有低高度 UICollectionView 的 UIRefreshControl

objective-c - 如何停止 AudioToolbox 的声音?

ios - 从现有应用程序中排除 iPad 支持

ios - Collection View 与其容器之间的 20px 间距

ios - 如何在滚动时隐藏和显示带有动画效果的标题 View