2014-02-18 95 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重置爲默認值方法。