Swift3 使用瀑布式布局和自调整单元格大小创建UIcollectionView

Swift3 使用瀑布式布局和自调整单元格大小创建UIcollectionView,swift3,uicollectionview,ios9,ios10,uicollectionviewlayout,Swift3,Uicollectionview,Ios9,Ios10,Uicollectionviewlayout,我正在尝试使用一个可以与iOS>=9.0配合使用的自调整大小的单元格来实现WaterwallLayout 类似pintrest collectionViewLayout的东西不是过渡,但区别在于我有固定比例的图像,但下面的字段是动态的,我使用了UIStackView 自我调整工作正常,但我的问题是将单元格原点更改为单元格之间始终=15 PX 因此,当我在没有任何定制的情况下尝试FlowLayout时,除了单元格之间的边距不正确外,其他一切都正常工作,但是对于iOS>=9.0,滚动工作正常,没有

我正在尝试使用一个可以与iOS>=9.0配合使用的自调整大小的单元格来实现WaterwallLayout 类似pintrest collectionViewLayout的东西不是过渡,但区别在于我有固定比例的图像,但下面的字段是动态的,我使用了
UIStackView

自我调整工作正常,但我的问题是将单元格原点更改为单元格之间始终=15 PX

因此,当我在没有任何定制的情况下尝试FlowLayout时,除了单元格之间的边距不正确外,其他一切都正常工作,但是对于iOS>=9.0,滚动工作正常,没有任何性能问题

在尝试定制FlowLayout后,iOS 10的性能非常差,但iOS 9的性能很好,而iOS 10的性能不好。我知道苹果在UICollectionView上做了一些改进,但我看不出有什么改进

我的手机:

 override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    self.setNeedsLayout()
    self.layoutIfNeeded()
    layoutAttributes.bounds.size.height = systemLayoutSizeFitting(layoutAttributes.size, withHorizontalFittingPriority: UILayoutPriorityRequired, verticalFittingPriority: UILayoutPriorityFittingSizeLevel).height
    return layoutAttributes
}
我的控制器:

ViewDidLoad {
  layoutView = collectionView.collectionViewLayout as! CustomLayout
        if #available(iOS 10.0, *) {
            self.collectionView.isPrefetchingEnabled = false
        }
        layoutView.estimatedItemSize = CGSize(width:      layoutView.cellWidth, height: 200)
 }

extension RecipesCollectionWithSortBar: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: customCell.reuseIdentifier, for: indexPath) as! customCell
    return cell
}
 extension RecipesCollectionWithSortBar : UICollectionViewDelegateFlowLayout {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.bounds.maxY == scrollView.contentSize.height &&  !isLoadingMoreRecipes {
       //
        LoadMore
    }
  }
我的自定义流布局

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attrs = super.layoutAttributesForElements(in: rect)
        var attrsCopy = [UICollectionViewLayoutAttributes]()

        for element in attrs! where (element.copy() as! UICollectionViewLayoutAttributes).representedElementCategory == .cell
            && element.frame.height != cacheCells[element.indexPath.item]?.frame.height {
            if element.indexPath.item == 0 {
                element.frame.origin.y = self.sectionInset.top
                element.frame.origin.x = self.sectionInset.left
            } else if element.indexPath.item == 1 {
                element.frame.origin.y = self.sectionInset.top
                element.frame.origin.x = cacheCells[0]!.frame.maxX + margin
            } else {
                let previous = cacheCells[element.indexPath.item - 2]
                element.frame.origin.y = previous!.frame.maxY + self.minimumInteritemSpacing
                element.frame.origin.x = previous!.frame.origin.x
            }
            cacheCells[element.indexPath.item] = element
            attrsCopy.append(element)
        }

        let values = cacheCells.values.filter({ $0.frame.intersects(rect) })
        return values.map({ $0 })
    }
这里我有很多问题: 1-我在collectionView的末尾有一个巨大的空白,因为我更改了单元格原点。这个问题只在iOS>10时发生。 我试图覆盖内容大小

func getContentHeight() -> CGFloat {
        if !cacheCells.values.isEmpty {
            let attribute = cacheCells.values.max { $0.0.frame.maxY < $0.1.frame.maxY }
            return attribute!.frame.maxY + sectionInset.bottom
        }
        return contHeight
    }
    override var collectionViewContentSize: CGSize {
        return CGSize(width: self.cellWidth * CGFloat(columnCount), height: getContentHeight())
    }
func getContentHeight()->CGFloat{
if!cacheCells.values.isEmpty{
让属性=cacheCells.values.max{$0.0.frame.maxY<$0.1.frame.maxY}
返回属性!.frame.maxY+sectionInset.bottom
}
返回高度
}
覆盖变量collectionViewContentSize:CGSize{
返回CGSize(宽度:self.cellWidth*CGFloat(columnCount),高度:getContentHeight())
}
由于某些原因,iOS 9不喜欢此解决方案,滚动停止

2-滚动性能在iOS 10中非常差,在iOS 9.0中最差

3-当我加载更多“信不信由你”时,它会滚动3或4次,并且在打印日志时没有更多的项目显示(我有100个项目,但集合视图仅显示70个),有时在硬滚动时,它会记住还有一些项目并显示它们。我不确定我做错了什么


两周来,我一直在努力找出问题所在,可能是有了一些新的眼光和思路可以指出我的问题。

这不是一个自动调整大小的单元格,而是两个视图控制器之间的自定义转换。我不是指单击单元格,我是指在collectionViewLayout中,单元格之间的间距