美文网首页
iOS之UICollectionView的创建与介绍

iOS之UICollectionView的创建与介绍

作者: 等不来的期待 | 来源:发表于2018-03-13 18:04 被阅读6次

    UICollectionView 在iOS开发中也是非常常用的控件,例如市面上最流行的淘宝,京东都有用到UICollectionView来写页面,一些好看的瀑布流也是采用UICollectionView来实现,总体来说UICollectionView用起来还是很实用方便的;
    UICollectionView创建

     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat itemWidth = (MWidth - 40) / 3;
        
        //设置单元格大小
        layout.itemSize = CGSizeMake(itemWidth, itemWidth / 1.6);
        //最小行间距(默认为10)
        layout.minimumLineSpacing = 10;
        //最小item间距(默认为10)
        layout.minimumInteritemSpacing = 10;
        //设置UICollectionView的滑动方向
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置UICollectionView的间距
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, MWidth, MHeight - 80 - 220 - 50 - 40) collectionViewLayout:layout];
        
        //遵循CollectionView的代理方法
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.backgroundColor = [UIColor clearColor];
        //注册cell
        [_collectionView registerClass:[DiscoverRechangeCenterCell class] forCellWithReuseIdentifier:@"DiscoverRechangeCenterCell"];
        [self.centerView addSubview:self.collectionView];
    

    UICollectionView的代理方法

    #pragma mark CollectionViewDelegate
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        
        return 1;
        
    }
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        
        return self.sourceArr.count;
        
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        
        DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
        
    //实现cell的展示效果
        
        return cell;
    }
    
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        //实现cell的点击方法
    }
    

    相关文章

      网友评论

          本文标题:iOS之UICollectionView的创建与介绍

          本文链接:https://www.haomeiwen.com/subject/qjttqftx.html