//按钮金额数组
@property (nonatomic,strong)NSArray *moneyArr;
//选中后添加的数组
@property (nonatomic,strong)NSMutableArray *rightLabelArray;
- (void)initCollectionView:(UIView *)veew {
_moneyArr = @[@"10",@"2",@"8",@"66",@"8",@"9",@"1",@"18"];
_rightLabelArray = [NSMutableArrayarray];
//先实例化一个层
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayoutalloc ]init ];
//创建一屏的视图大小
UICollectionView *collectionView=[[UICollectionViewalloc ] initWithFrame :self.view.boundscollectionViewLayout:layout];
collectionView.frame=CGRectMake(0,80,ScreenWidth,200);
[collectionViewregisterClass:[UICollectionViewCellclass ] forCellWithReuseIdentifier :colletionCell];
collectionView.backgroundColor = [UIColorclearColor];
collectionView. delegate = self ;
collectionView. dataSource = self ;
[veew addSubview :collectionView];
}
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger )section {
return _moneyArr.count ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UINib *nib = [UINibnibWithNibName:@"llectionViewCell"bundle: [NSBundlemainBundle]];
[collectionViewregisterNib:nibforCellWithReuseIdentifier:colletionCell];
llectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:colletionCell forIndexPath:indexPath];
cell.textLable.text =_moneyArr[indexPath.item];
// 选中一个的方法
if (_rightLabelArray.count ==0) {
// 第一次加载的方法
if (indexPath.row ==0 ) {
cell.backgroundColor = [UIColorredColor];
}else{
cell.backgroundColor = [UIColorwhiteColor];
}
}else{
if ([_rightLabelArray containsObject:_moneyArr[indexPath.item]]) {
cell.backgroundColor = [UIColorredColor];
} else {
cell.backgroundColor = [UIColorwhiteColor];
}
}
return cell;
}
//UICollectionView被选中时调用的方法
- ( void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%zd",indexPath.row);
//点击后添加因为只需要一个所以直接初始化掉重新赋值
_rightLabelArray = [NSMutableArrayarrayWithObject:_moneyArr[indexPath.item]];
[collectionView reloadData];
}
//定义每个UICollectionView的大小
- ( CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
returnCGSizeMake (70 ,70 );
}
//定义每个UICollectionView的边距
- ( UIEdgeInsets )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger )section {
returnUIEdgeInsetsMake (10 , 10 ,10 ,10 );
}