[英]UICollectionView cells overlapping swift
I am creating a UICollectionView, but when a new cell is added to the view it is partially overlapping the previous cell.我正在创建一个 UICollectionView,但是当一个新单元格添加到视图时,它与前一个单元格部分重叠。 Below is my code:
下面是我的代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
cell.frame.size.width = self.view.frame.width / 3
cell.frame.size.height = self.view.frame.height / 4
cell.backgroundView = imageView
return cell
}
How do I make sure the cells don't overlap?如何确保单元格不重叠?
You shouldn't assign the frame value by yourself in the cellForItemAtIndexPath
method.您不应在
cellForItemAtIndexPath
方法中自行分配框架值。
The more suitably way is to do this in the UICollectionViewLayout
, then set it as the collectionview's layout property.更合适的方法是在
UICollectionViewLayout
执行此UICollectionViewLayout
,然后将其设置为 collectionview 的布局属性。 Actually, you need a UICollectionViewLayout
instance when you init the UICollectionView
instance.实际上,当您初始化
UICollectionViewLayout
实例时,您需要一个UICollectionView
实例。
Or simply, use the UICollectionViewFlowLayout
which system provides to implement the flow layout conveniently, tell it the size of each cell, spaces between them, and some other informations by its delegate.或者干脆使用系统提供的
UICollectionViewFlowLayout
来方便的实现流布局,通过它的delegate告诉它每个单元格的大小,单元格之间的空间,以及其他一些信息。 The layout instance will arrange all for you.布局实例将为您安排一切。
For example.例如。
class MYViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//In viewDidLoad()
var collectionViewFlowLayout = UICollectionViewFlowLayout()
var myCollectionView = UICollectionView(frame: self.view.bounds, layout: collectionViewFlowLayout)
// A flow layout works with the collection view’s delegate object to determine the size of items, headers, and footers in each section and grid.
// That delegate object must conform to the UICollectionViewDelegateFlowLayout protocol.
myCollectionView.delegate = self
myCollectionView.dataSource = self
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(10, 10);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10;
}
}
All what you need is to extend you ViewController from UICollectionViewDelegateFlowLayout
and implement sizeForItemAt
:你需要做的就是从
UICollectionViewDelegateFlowLayout
扩展你的 ViewController 并实现sizeForItemAt
:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (collectionView == myCollectionView_1) {
return CGSize(width: self.view.frame.width / 3, height: self.view.frame.height / 4)
} else {
return collectionView.frame.size
}
}
对我来说,我只需在集合视图选项中将“估计大小”更改为“无”即可解决问题,如此处的屏幕截图所示:屏幕截图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.