uicollectionView

本文介绍如何使用UICollectionView展示视频数据,并实现上下拉刷新加载更多功能。包括UICollectionView的初始化配置、UICollectionViewFlowLayout的设置、MJRefresh组件的应用及自定义视频单元格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];

        _collectionView.backgroundColor =[UIColor clearColor];
        [_collectionView registerClass:[BlocVideoViewCell class] forCellWithReuseIdentifier:BLOC_TAG_VIDEO_CELL];
        
        MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (self.refreshBlock) {
                self.refreshBlock();
            }
        }];
        _collectionView.mj_header = header;
        
        MJRefreshAutoNormalFooter * footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if (self.loadMoreBlock) {
                self.loadMoreBlock();
            }
        }];
        _collectionView.mj_footer = footer;
    }
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    return _collectionView;
}

#pragma mark -UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(weakSelf);
    BlocVideoViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:BLOC_TAG_VIDEO_CELL forIndexPath:indexPath];
    cell.pushIndexToVideoPlayerBlock = ^(NSInteger index){
        [weakSelf handelIntoVideoVC:index];
    };

     BlocVideoModel * videoMode = [self.dataArray objectAtIndex:indexPath.row];
    [cell loadVideoData:videoMode withIndex:indexPath.row];
//    cell.layer.borderColor = [[UIColor redColor]CGColor];
//    cell.layer.borderWidth = 1;
//    cell.backgroundColor = [UIColor greenColor];

    return cell;
}

- (void)handelIntoVideoVC:(NSInteger )index{
    
    BlocVideoModel * dataModel = [self.dataArray objectAtIndex:index];
    if (self.pushToVideoPlayerBlock) {
        self.pushToVideoPlayerBlock(dataModel);
    }
//        NSLog(@"cellbloc传的第几个-->%zd",index);
}
#pragma mark -UICollectionViewDelegateFlowLayout
//设置每个单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger columnsNum = 2;
    NSInteger collectionViewWidth = self.collectionView.bounds.size.width;
    CGFloat itemWidth =(collectionViewWidth -14-18*BLOC_W_ADJUST* (columnsNum -1))/columnsNum;
//    NSLog(@"cell的宽--%f,collectionViewWidth宽-->%ld",itemWidth,(long)collectionViewWidth);
       return CGSizeMake(itemWidth, 100*BLOC_H_ADJUST);
}
//设置纵向的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
        return 0;
    
}

//设置单元格间的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
 
    return 18*BLOC_W_ADJUST;
}
////通过调整inset使单元格顶部和底部都有间距(inset次序: 上,左,下,右边)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 7, 0, 7);
}

#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    BlocVideoModel * dataModel = [self.dataArray objectAtIndex:indexPath.row];
    if (self.pushToVideoPlayerBlock) {
        self.pushToVideoPlayerBlock(dataModel);
    }
//    NSLog(@"dijihang-->%zd",indexPath.row);
}

、、-----------------

在cell的   contentView中添加view

[self.contentView addSubview:self.videoBack];
[self.contentView addSubview:self.personLabel];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值