1.问题描述
最近在使用UICollectionView的时候,发现列表都正常显示了,但如果列表的数据的数量超过一屏时,第一个cell竟然数据重叠了,而且往下滑动的时候,有空行存在,网上的答案也是众说纷纭,无奈只能自行尝试,耗费的时间比较久的。


2.解决办法
上述其实是两个问题:重叠和空cell。
2.1空白cell。
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//flowLayout.estimatedItemSize = CGSizeMake(1, 1);
因为我的CELL的高度都是固定的,所以不需要estimatedItemSize。加上了反而会导致CELL的大小计算不准,导致CELL偏移。
2.2 cell重叠
self.parent = [MyRelativeLayout new];
self.parent.myWidth = MyLayoutSize.fill;
self.parent.myHeight = MyLayoutSize.fill;
//重点
self.parent.clipsToBounds = YES;
self.parent.tag = 100;
[self.parent setTarget:self action:@selector(onViewClick:)];
[self.contentView addSubview:self.parent];
如上代码,我加上
clipsToBounds
属性竟然奇迹般的解决了cell重叠的问题,很是奇怪。When YES, content and subviews are clipped to the bounds of the view. Default is NO.
3.总结
代码中,MyRelativeLayout
是第三方库自动布局MyLinearLayout里的用法。我的cell重叠的问题可能是因为我没有指定cell的大小,而是在复用的过程中自动计算的,加上clipsToBounds
属性也许使它填充父空间的属性生效了。
网友评论