简体   繁体   English

UICollectionView 单元格重叠迅速

[英]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;
    }
}

For more infomation, read the doc .有关更多信息,请阅读文档

For me it was the wrong cell width and height specified in the size inspector of the view controller.对我来说,这是在视图控制器的大小检查器中指定的错误单元格宽度和高度。 Just set that properly possibly the same as your nib file and it may work.只需正确设置它可能与您的 nib 文件相同,它就可以工作。

Please see screenshot for clarity.为清楚起见,请查看屏幕截图。 在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM