刚开始定义CollectionViewCell的时候是用UIButton,无奈单选没效果。最后用UILabel,然后单选就可以了,好奇怪,以后在纠结其原因吧。
先记录线单选的操作流程,如下:
1 . cell的部分代码:
在cell中设置UILabel 的默认颜色252525(灰色)
#define cellWidths ((DEVICEWIDTH -60)/4.0)@implementation FilterCollectionViewCell- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self){self.backgroundColor = COLOR_EFEFEF;_carLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellWidths,32)];_carLabel.layer.masksToBounds = YES;_carLabel.layer.cornerRadius = 4;_carLabel.textAlignment = NSTextAlignmentCenter;[_carLabel setFont:[UIFont systemFontOfSize:14.0]];<span style="color:#ff0000;"> [_carLabel setTextColor:COLOR_252525];</span>[self addSubview:_carLabel];}return self;
}
@end
2. 初始化UICollectionView
- (void)initCCollectionView
{UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];[layout setScrollDirection:UICollectionViewScrollDirectionVertical];layout.minimumInteritemSpacing = 0;layout.minimumLineSpacing = 0;_caCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 111-64, DEVICEWIDTH -30, 79) collectionViewLayout:layout];_caTypeCollectionView.backgroundColor = [UIColor clearColor];_carTypeCollectionView.delegate = self;_carTypeCollectionView.dataSource = self;[_totalScrollView addSubview:_carTypeCollectionView];[_caTypeCollectionView registerClass:[<span style="font-family: Arial, Helvetica, sans-serif;">FilterCollectionViewCell</span><span style="font-family: Arial, Helvetica, sans-serif;"> class] forCellWithReuseIdentifier:@"carTypeCollectionView"];</span>
}
3. 设置选择cell时,UILabel的背景颜色
(1)中的红色设置选择当前cell时高亮当前的背景色
(2)没有选中的其他全部cell的背景色设置为EFEFEF(灰色)
(3)选中的cell其背景色设置为3B98E0
#pragma mark -- UICollectionViewDataSource- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{return 1;
}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{if (collectionView == _carTypeCollectionView){return _carTypeArray.count;}else if (collectionView == _carLengthCollectionView){return _carLengthArray.count;}return 0;
}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{if (collectionView == _carTypeCollectionView){FilterCollectionViewCell *cell = (<span style="font-family: Arial, Helvetica, sans-serif;">FilterCollectionViewCell</span> *)[collectionView dequeueReusableCellWithReuseIdentifier:@"carTypeCollectionView" forIndexPath:indexPath];cell.layer.cornerRadius=4;if ([_carTypeArray count] > indexPath.row){[cell.carLabel setText:_carTypeArray[indexPath.row]];}1.<span style="color:#ff0000;"> UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.frame];selectedBGView.backgroundColor = TFCOLOR_3B98E0;selectedBGView.layer.cornerRadius=4;cell.selectedBackgroundView = selectedBGView;</span>return cell;}return nil;
}#pragma mark --UICollectionViewDelegateFlowLayout- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{return CGSizeMake(cellWidths, 32);
}//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{return 10;
}//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{return 15;
}#pragma mark -- UICollectionViewDelegate
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{if (collectionView == _carTypeCollectionView){if ([_carTypeArray count] > indexPath.row){[_carTypeLabel setText:_carTypeArray[indexPath.row]];}}else if (collectionView == _carLengthCollectionView){if ([_carLengthArray count] > indexPath.row){[_carLengthLabel setText:_carLengthArray[indexPath.row]];}}
}- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{if (collectionView == _carTypeCollectionView){UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];2.<span style="color:#ff0000;"> [cell setBackgroundColor:TFCOLOR_EFEFEF];</span>}
}- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{if (collectionView == _carTypeCollectionView){UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];3.<span style="color:#ff0000;">[cell setBackgroundColor:TFCOLOR_3B98E0];</span>}
}