iOS的 UICollectionView使用

UICollectionView的基本使用
先声明一个

@property (nonatomic) UICollectionView * collectionView;

然后

-(UICollectionView*)collectionView{
    if (!_collectionView) {
        //创建一个layout布局类
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //头部视图设置悬浮
        layout.sectionHeadersPinToVisibleBounds = YES;
        //设置每个item的大小为120*180
        layout.itemSize = CGSizeMake(120, 180);
        _collectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationHeight, SCREEN_WIDTH,SCREENH_HEIGHT-NavigationHeight-HEIGHT_VALUE(40)) collectionViewLayout:layout];
        _collectionView.delegate =self;
        _collectionView.dataSource=self;
        //注册ImgBillCell
        [_collectionView registerClass:[ImgBillCell class] forCellWithReuseIdentifier:@"cellImg"];
        _collectionView.backgroundColor = HEXCOLOR(0xF2F2F2);
        //注册头部视图
        [_collectionView registerClass:[ZgoodSearchListHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
        //注册底部视图
        [_collectionView registerNib:[UINib nibWithNibName:@"MGFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifierFooter];
                //下拉刷新
         _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        [_collectionView.mj_header beginRefreshing];
    }
    return _collectionView;
}

然后在viewDidLoad调用

- (void)viewDidLoad {
      _dataArray = [[NSMutableArray alloc] init];
      [super viewDidLoad];
      [self addSubview:self.collectionView];
   }

实现代理方法

#pragma mark - UICollectionViewDelegate
//UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
//的Section的个数
-( NSInteger )numberOfSectionsInCollectionView:( UICollectionView *)collectionView{
    return 1 ;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //每个item的内容继承UICollectionViewCell
    ImgBillCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellImg" forIndexPath:indexPath];
    cell.parentView = self;
    if (!cell) {
        cell = [[ImgBillCell alloc] initWithFrame:CGRectZero];
    }
    ZItemDTO* itemInfo = [_dataArray objectAtIndex:indexPath.row];
    ZOrderDTO* dto = [_itemCountDic objectForKey:[itemInfo.itemId description]];
    [cell bindDTO:itemInfo orderDto:dto];
    return cell;
}


#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row);
}

//返回这个UICollectionViewCell是否可以被选择
-( BOOL )collectionView:( UICollectionView *)collectionView shouldSelectItemAtIndexPath:( NSIndexPath *)indexPath{
    return YES ;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
 UICollectionReusableView *supplementaryView;
 
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
 //头部视图
      ZgoodSearchListHeader *view = (ZgoodSearchListHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                            withReuseIdentifier:@"UICollectionViewHeader"
                                                                                   forIndexPath:indexPath];
       supplementaryView = view;
 
     }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
     //同上
     }

    return supplementaryView;
}
//头部视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(kScreenWidth, 45);
}

// 设置底部视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
 return CGSizeMake(kScreenWidth, 50);
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float w=(SCREEN_WIDTH-24)/3;
    return CGSizeMake(w,w+HEIGHT_VALUE(40));
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
     return UIEdgeInsetsMake(6, 6, 6, 6);
}

//设置横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 6;
}
//设置纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 6;
}

-(void)loadNewData{
 //请求数据将请求到数据给_dataArray
}

上拉加载跟多

_collectionView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
[_collectionView.mj_footer endRefreshing];

局部刷新

[self.collectionView reloadItemsAtIndexPaths:indexPath];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值