常用UI

UICollectionView:

前言:它和UITableView很相似,但它要更加强大,UITableView的布局形式比较单一,局限于行列表,而UICollectionView的强大之处在于把视图布局分离出来成为一个独立的类,你想实现怎样的视图布局,就子类化这个类并在其中实现。

UICollectionView基础:

UICollectionViewFlowLayout:是系统为我们写好的布局类,该类为我们提供了一个简单的布局样式,假如我们只需要一个特别简单的网格布局或者流水布局,可以直接使用它,它继承自UICollectionViewLayout

自定义布局:继承自UICollectionViewLayout

1.创建UICollectionView视图:

staticNSString*constcellId =@"cellId";

_customLayout = [[CustomCollectionViewLayout alloc] init];// 自定义的布局对象_collectionView = [[UICollectionViewalloc] initWithFrame:self.view.bounds collectionViewLayout:_customLayout];

_collectionView.dataSource =self; 

 _collectionView.delegate =self;

 [self.view addSubview:_collectionView];

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];

2.自定义布局常用api

//预布局方法 所有的布局应该写在这里面在这里面写好布局方法

- (void)prepareLayout

//代理方法 此方法我们应该返回当前屏幕正在显示的视图(cell 头尾视图)的布局属性集合(返回UICollectionViewLayoutAttributes 对象数组)

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect

//插入cell的时候系统会调用改方法

- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath

注意:  NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];

              NSIndexPath *path2 = [NSIndexPath indexPathForRow:0 inSection:0];

 NSLog(@"%@--%@--%d",path,path2,[pathisEqual:path2]); //输出:0xc000000000000016--0xc000000000000016--1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容