我有一个包含两个部分的 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];
最佳答案
为设置了部分条件的 cellForItemAtIndexPath
的 else
部分编写相同的代码段
关于ios - objc 如何更改 UICollectionView 中第一行单元格的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178011/