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

标签 ios swift uicollectionview

所以正如标题所暗示的那样,我似乎遇到了一个奇怪的问题。我在这里要做的就是创建一个 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/

相关文章:

ios - CGContextStrokePath 颜色

ios - 如何从 iOS 中的流接收数据 (swift)

ios - 禁用 UICollectionView 顶部的 "pull to refresh"空间

ios - 如何使用 UICollectionView 防止滚动 UIScrollView

ios - 如何跟踪 Sprite Kit 中的动画

iphone - 在 TableView 中重新排序单元格时获取索引路径?

ios - Swift 错误 : '(AFHTTPRequestOperation! , NSError? ) -> is not convertible to ' ((AFHTTPRequestOperation? , NSError) -> Void?

arrays - 生成具有 Int 范围的特定 float 组

ios - 如何将 UIActivityIndi​​catorView 置于 UICollectionViewCell 的中心?

ios - 在 UICollectionView 中指定单元格之间的宽度和边距