UICollectionView 单选的实现

刚开始定义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>
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值