IOS -- UICollectionView里面的cell點擊,點擊一個cell改變其他cell的狀態


view.m

- (void)initWithCreatorCollectView
{
    //模擬數據
    [self imitateData];
    
    float width = ([UIScreen mainScreen].bounds.size.width - 55)/2.0;
    float height = 45;
    float rowCount = _buyBtnDataArr.count / 2.0;
    _collecViewHeight = ceilf(rowCount) * (height + 15);
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumInteritemSpacing = 13;//左右邊距
    _flowLayout.minimumLineSpacing = 5;//上下邊距
    _flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 0, 1);
    //設置collectionView滾動方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _flowLayout.itemSize = CGSizeMake(width, 60);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-40, _collecViewHeight) collectionViewLayout:_flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[HMSemiBuyBtnCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.delaysContentTouches = NO;//使view里面的btn點擊明顯
    _collectionView.scrollEnabled = NO;//不可滑動
    _collectionView.showsVerticalScrollIndicator = NO;//隱藏滾動條
    
    
    //設置默認選中按鈕的樣式
    [_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]  animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    
    
    [self addSubview:_collectionView];
}

cell.m

- (void)setSelected:(BOOL)selected
{
    NSLog(@"%@",selected?@"YES":@"NO");
  
    if (selected) {//選中
        [self.buyChapterBtn setUserInteractionEnabled:NO];
        [self.buyChapterBtn setTitleColor:[UIColor colorWithHexString:@"F94F50"] forState:UIControlStateNormal];
        self.buyChapterBtn.layer.borderColor = [UIColor colorWithHexString:@"F94F50"].CGColor;
        NSLog(@"asasasas-1");
    }else {//未選中
        [self.buyChapterBtn setUserInteractionEnabled:YES];
        [self.buyChapterBtn setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
        self.buyChapterBtn.layer.borderColor = [UIColor colorWithHexString:@"D6D6D6"].CGColor;
        NSLog(@"asasasas-2");
    }
    [super setSelected:selected];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM