gpt4 book ai didi

ios - UICollectionViewDelegateFlowLayout Edge Insets 未被 sizeForItemAt 函数识别?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:03 28 4
gpt4 key购买 nike

所以正如标题所暗示的那样,我似乎遇到了一个奇怪的问题。我在这里要做的就是创建一个 2 列的 Collection View ,而无需将任何内容硬编码到我的委托(delegate)方法中。调试后我发现 insetForSectionAt 在 sizeForItemAt 之后调用,因此在计算每个单元格的大小时不考虑自定义插图。

extension ViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
print("left: ", flowLayout.sectionInset.left) // Prints out 0.0
print("right: ", flowLayout.sectionInset.right) // Prints out 0.0
let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing * (cellsPerRow - 1)
let itemWidth = (collectionView.bounds.size.width - marginsAndInsets) / cellsPerRow
return CGSize(width: itemWidth, height: itemWidth)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
}

这个问题可以通过将值直接硬编码到 itemWidth 变量中来非常丑陋地解决,-20 因为我知道左右插图的值,10 + 10

let itemWidth = (collectionView.bounds.size.width - marginsAndInsets - 20) / cellsPerRow

但是,我必须相信有更好的方法可以做到这一点,我如何在 UIEdgeInset 计算完成后调用 reloadData 以便我的单元格大小合适?

最佳答案

这就是我最终所做的,我只是在为 collectionView 及其单元格创建大小时直接添加了自定义项。

class PinController: UICollectionViewController {

fileprivate let pinCellId = "pinCellId"
fileprivate let cellsPerRow: CGFloat = 2.0
fileprivate let margin: CGFloat = 10.0
fileprivate let topMargin: CGFloat = 2.0
}


extension PinController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let flowLayout = customize(collectionViewLayout, margin: margin)
let itemWidth = cellWidth(collectionView, layout: flowLayout, cellsPerRow: cellsPerRow)
return CGSize(width: itemWidth, height: itemWidth * 1.1)
}

/*
Customizes the layout of a collectionView with space to the left and right of each cell that is
specified with the parameter margin
*/
private func customize(_ collectionViewLayout: UICollectionViewLayout, margin: CGFloat) -> UICollectionViewFlowLayout {
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
// Interitem spacing affects the horrizontal spacing between cells
flowLayout.minimumInteritemSpacing = margin
// Line spacing affects the vertical space between cells
flowLayout.minimumLineSpacing = margin
// Section insets affect the entire container for the collectionView cells
flowLayout.sectionInset = UIEdgeInsets(top: topMargin, left: margin, bottom: 0, right: margin)
return flowLayout
}

/*
Calculates the proper size of an individual cell with the specified number of cells in a row desired,
along with the layout of the collectionView
*/
private func cellWidth(_ collectionView: UICollectionView, layout flowLayout: UICollectionViewFlowLayout, cellsPerRow: CGFloat) -> CGFloat {
// Calculate the ammount of "horizontal space" that will be needed in a row
let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing * (cellsPerRow - 1)
let itemWidth = (collectionView.bounds.size.width - marginsAndInsets) / cellsPerRow
return itemWidth
}
}

关于ios - UICollectionViewDelegateFlowLayout Edge Insets 未被 sizeForItemAt 函数识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com