要实现的效果:

因为这个Cell会出现在系统很多地方,所以这个Cell新建在Main的View中

继承自UICollectionViewCell 命名为 CollectionNormalCell

来到CollectionNormalCell XIB进行布局
先对能确定的位置的图标进行布局约束,
拖入图片和lable进行布局

接下来图片,进行布局约束,图片向下的约束相对与图标进行约束
相对于已经确定的图标进行布局


给图片进行设置圆角,需要设置两个属性
layer.cornerRadius
layer.masksToBounds

设置图片未加载的默认图片

继续创建布局约束

微调布局

完整效果

回到之前RecommendViewController注册Cell的地方,替换成XIB的注册Cell形式

运行效果

定制个性的Cell 样式
新建CollectionPrettyCell 继承自 collectionViewCell
拖入buttom进行布局,设置图标和字体大小,设置约束条件

布局效果:先确定最下面的城市的buttom的布局约束,如果要做相对布局约束就要设置高度,给文字设置一个高度,城市的布局约束,下面5,左边0设置高度
对标题进行布局,设置左边为0,下面为5 设置高度,设置图片,上左右都是0 ,下相对与标题进行设置相对布局约束

给图片进行设置圆角,需要设置两个属性
layer.cornerRadius
layer.masksToBounds
回到RecommendViewController
添加

注册cell

在设置Cell的地方进行判断获取哪个Cell

设置collectionView的代理

遵守协议

实现协议的方法:

运行效果为:第二组与第一组的显示样式不同

网友评论