OC CollectionView笔记

自定义layout


- (void)prepareLayout{

    [super prepareLayout];
    
    ///布局,计算出每个item的frame
    //保存进attrs数组
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

    return _attrs;//返回每个item的布局,数组
}

- (CGSize)collectionViewContentSize{
 
    NSArray *tempArray = [_infos sortedArrayUsingSelector:@selector(compare:)];
    NSNumber *maxNum = tempArray.lastObject;
    return CGSizeMake(self.collectionView.bounds.size.width, maxNum.floatValue);//
}

  • 算出有多少列
  • 根据宽,indexPath计算x,y
  • 保存最大的y值,返回collection的contentSize

上拉加载/无缝请求

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //在这个回调里面判断,是否达到请求数据的界
    //而且要判断滑的方向
    //请求不能重复
}

自定义Cell

  • - (instancetype)initWithFrame:(CGRect)frame方法里自定义

  • - (void)layoutSubviews里才能获取到frame

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容