UICollectionViewCell更改背景颜色

时间:2014-02-18 13:39:00

标签: iphone objective-c uicollectionview

我有UICollectionview部分和两个单元格类。

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

myCell *cell;

    firstDayInMonth = [self dayWeekStart:[self getDateFromItem:dateFromStart section:indexPath.section row:1]];

    if (indexPath.row < firstDayInMonth) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendarEmpty" forIndexPath:indexPath];
    } else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendar" forIndexPath:indexPath];
    }
}

我有开始项目,部分和期间。从开始部分和项目开始,我需要根据周期更改背景颜色单元格。我只需要更改cellCalendar。我使用cellCalendarEmpty先移动cellCalendar

1 个答案:

答案 0 :(得分:0)

通过在配置时处理案例的两个分支(所有情况)进行修复,例如:

if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){

    cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
    cell.titleLabel.textColor = [UIColor whiteColor];
} else {
    cell.backgroundColor = [UIColor whiteColor];  // whatever the default color is
}

如果您正在使用UICollectionViewCell的自定义子类,则还可以通过实现prepareForReuse方法重置为默认值。