关于集合视图UICollectionView

来源:互联网 发布:最流行编程语言 编辑:程序博客网 时间:2024/05/17 01:55

     关于集合视图UICollectionView,现在应该是Apple主推的一个类,用的也越来越多,所以一定要像UITableView一样非常熟才行.下面详细述说我目前知道的东西.

    

    //初始化UICollectionView

      

@interface RecommendView :UIView<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,NetWorkRequestDelegate>

   

//系统给的样式,瀑布流

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayoutalloc]init];

//设置横向(左右)滚动,默认是上下

    layout.scrollDirection =UICollectionViewScrollDirectionVertical;

  //设置大小

//    layout.itemSize = CGSizeMake(100, 100);

//    //行之距

//    layout.minimumLineSpacing = 50;

//    //列之距

//    layout.minimumInteritemSpacing=50;

//    //距离顶头

//    layout.headerReferenceSize = CGSizeMake(100, 100);

//    //距离结尾

//    layout.footerReferenceSize = CGSizeMake(100, 100);

 //把整个section往里缩了(,,,)

    layout.sectionInset = UIEdgeInsetsMake(20,20,20,20);


//第一步,初始化创建集合视图对象


   UICollectionView * _collectView  = [[UICollectionViewalloc]initWithFrame:CGRectMake(0,0,320,350)collectionViewLayout:layout];

  

 //第二步,设置代理

    _collectView.dataSource =self;

    _collectView.delegate =self;


    

   // 注册可重用视图的标示

    [_collectViewregisterClass:[ProductCellclass]forCellWithReuseIdentifier:@"cell"];

    [_collectViewregisterClass:[Samecat_RecommendCellclass]forCellWithReuseIdentifier:@"cell1"];

    [_collectViewregisterClass:[PhotoSupplementaryViewclass]                 forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"head"];


//定义展示的Section的个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 2;

}

//定义展示的UICollectionViewCell的个数

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

{

    if (section == 0) {

         return [_dataAraycount];

    }

    return [_dataArray1count];

}

//定义每个UICollectionView 的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(130,180);

}



//定义每个UICollectionView margin(,,,)

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

{

    returnUIEdgeInsetsMake(10,20,10,20);

}


//每个UICollectionView展示的内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    

    

    if (indexPath.section ==0) {

        _item = [_dataArayobjectAtIndex:indexPath.row];

        static NSString * identIfer =@"cell";

        ProductCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:identIferforIndexPath:indexPath];

        cell.item = _item;

        return cell;

    }

    _item1 = [_dataArray1objectAtIndex:indexPath.row];

    static NSString * identIfer =@"cell1";

    Samecat_RecommendCell * cell1 = [collectionViewdequeueReusableCellWithReuseIdentifier:identIferforIndexPath:indexPath];

    cell1.item = _item1;

    

    return cell1;

}


//每个UICollectionViewsection(区头)的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

    return CGSizeMake(320,40);

}


//每个UICollectionViewsection(区头)展示的内容

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    

    PhotoSupplementaryView * header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"head"forIndexPath:indexPath];;

    if (indexPath.section ==0) {

        

        header.headerLabel.text =@"看过此商品的顾客最终购买";

    }

    if (indexPath.section ==1) {

         header.headerLabel.text =@"同类产品热销";

    }

    return header;

}


//UICollectionView被选中时调用的方法

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






  



  







0 0
原创粉丝点击