CollectionView点击cell变色
在// cell显示内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [NSArray arrayWithObjects:@"今天", @"明天", @"后天", nil];
DayBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dayBtnCell" forIndexPath:indexPath];
cell.lab.text = arr[indexPath.row];
// cell点击变色
UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];
selectedBGView.backgroundColor = kMainColor;
cell.selectedBackgroundView = selectedBGView;
return cell;
}
// cell点击变色
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// cell点击变色
- (void)collectionView:(UICollectionView *)colViewdidUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
if ([colView isEqual:self.dayCollection]) {
DayCollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:UIColorFromRGB(0xE6E6E6)];
} else {
DayBtnCollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:UIColorFromRGB(0xC8C8C8)];
}
}