2014-02-18 347 views
1

我有UICollectionview分区和两个单元类。UICollectionViewCell更改背景颜色

- (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

回答

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重置为默认值方法。