UICollectionView的基本使用

78 阅读1分钟

<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) UICollectionView *collectionView;

  • (void)viewDidLoad {     UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;     _collectionView =      [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, WEIDTH,HEIGHT- 100) collectionViewLayout:layout];

    _collectionView.backgroundColor =              [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0alpha:1];

    _collectionView.dataSource = self;

    _collectionView.delegate = self;

    [_collectionView registerClass:[JFCollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];

    _collectionView.showsVerticalScrollIndicator=NO;

    [self.view addSubview:_collectionView]; }

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{     return 3; }

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{    return _JFArr.count;     }

  • (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{         JFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid"forIndexPath:indexPath];

        cell.clipsToBounds = YES;         if (_JFArr.count>0) {             NSString *photo=_JFArr[indexPath.row];

            NSString *number=_JPArr[indexPath.row];

            cell.topLabel.text=photo;

            cell.bottomLabel.text=number;

        }else{

            [self getDate1];

        }         cell.layer.cornerRadius = 5;

        cell.backgroundColor = [UIColor whiteColor];

        cell.layer.borderColor = [UIColor colorWithRed:250/255.0 green:148/255.0 blue:55/255.0 alpha:1].CGColor;

        return cell; }

//item对应的上 左 下 右 的距离。

  • (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{     return UIEdgeInsetsMake(10, 10, 10, 10); }

//点击选定

  • (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{     if (indexPath.section==0) {

        JFCollectionViewCell *cell = (JFCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

        cell.backgroundColor=[UIColor colorWithRed:27/255.0 green:166/255.0 blue:152/255.0 alpha:1];

        cell.topLabel.textColor=[UIColor whiteColor];

        cell.bottomLabel.textColor=[UIColor whiteColor];

        self.isSelected=[NSString stringWithFormat:@"%ld",(long)indexPath.row];

        

        NSLog(@"第%ld区,第%ld个",(long)indexPath.section,(long)indexPath.row);

    }   

}

//取消选定

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {

        JFCollectionViewCell *cell = (JFCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

        cell.backgroundColor=[UIColor whiteColor];

        cell.topLabel.textColor=[UIColor colorWithRed:27/255.0 green:166/255.0 blue:152/255.0 alpha:1];

        cell.bottomLabel.textColor=[UIColor blackColor];

        NSLog(@"1第%ld区,1第%ld个",(long)indexPath.section,(long)indexPath.row);

    }

}

//item的大小

-(CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{     return CGSizeMake(WEIDTH/2.0-20, 80);

  }