[英]UICollectionViewCell change background color
I have UICollectionview
with sections and two cell class. 我有带有部分和两个单元格类的
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];
}
}
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
. 我只需要更改
cellCalendar
。 I use cellCalendarEmpty
to move first cellCalendar
. 我使用
cellCalendarEmpty
移动第一个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. 如果您使用的是UICollectionViewCell的自定义子类,则还可以通过实现prepareForReuse方法将其重置为默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.