简体   繁体   中英

UICollectionViewCell change background color

I have UICollectionview with sections and two cell class.

- (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];
    }
}

I have start item, section and period. Beginning from the start section and item i need to change background color cell depending on period. i need to change only cellCalendar . I use cellCalendarEmpty to move first cellCalendar .

Fix by handling both branches of the case (all cases) when you configure, eg:

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
}

If you're using a custom subclass of UICollectionViewCell, you may also reset to defaults by implementing the prepareForReuse method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM