gpt4 book ai didi

swift - 为什么不调用 UICollectionView insetForSectionsAtIndex?

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:03 24 4
gpt4 key购买 nike

我想使用以下函数,但不幸的是它没有被调用。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlowLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let cellCount = Double(collectionView.numberOfItems(inSection: section))
if let cell = collectionView.cellForItem(at: IndexPath(row: 0, section: section))
{
let cellWidth = Double(cell.frame.size.width)
let totalCellWidth = cellWidth * cellCount
let cellSpacing = Double(collectionViewLayout.minimumInteritemSpacing)
let totalSpacingWidth = cellSpacing * (cellCount - 1)

let leftInset = (collectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
let rightInset = leftInset

return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
}

return collectionView.layoutMargins
}

我收到以下控制台错误,但我的单元格小于我的 collectionView,我已将部分插入和布局边距设置为 0。

2017-03-30 11:25:14.624 Prep[92400:1862856] The behavior of the UICollectionViewFlowLayout is not defined because: 2017-03-30 11:25:14.624 Prep[92400:1862856] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 2017-03-30 11:25:14.624 Prep[92400:1862856] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {0, 68.5}> collection view layout: . 2017-03-30 11:25:14.625 Prep[92400:1862856] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. cell height --> 42.0

enter image description here

协议(protocol)

class OverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

class OverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

enter image description here

最佳答案

好的,没有调用insetForSectionAt的问题是你必须这样实现它:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

//access to minimumInteritemSpacing by casting to UICollectionViewFlowLayout
let layout = collectionViewLayout as! UICollectionViewFlowLayout
let spacing = layout.minimumInteritemSpacing

return UIEdgeInsetsMake(0.5,0,0.5,0)

}

看看 UICollectionViewDelegateFlowLayout 协议(protocol)。 insetForSectionAt 声明如下:

@available(iOS 6.0, *)
optional public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

所以你需要第一个可选参数来实现这个功能。

关于swift - 为什么不调用 UICollectionView insetForSectionsAtIndex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43114011/

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