我想使用以下函数,但不幸的是它没有被调用。
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
}
我遇到了以下控制台错误,但是我的单元格比我的集合视图小,并且我已将部分插图和布局边距设置为0。
2017-03-30 11:25:14.624 Prep[92400:1862856] UICollectionViewFlowLayout的行为未定义,因为: 2017-03-30 11:25:14.624 Prep[92400:1862856] 单元格高度必须小于UICollectionView减去部分插图上下值、减去内容插图上下值。 2017-03-30 11:25:14.624 Prep[92400:1862856] 相关的UICollectionViewFlowLayout实例为,它附加在;层=;contentOffset:{0,0};contentSize:{0,68.5}> collection view layout:。 2017-03-30 11:25:14.625 Prep[92400:1862856] 在调试器中设置一个符号断点UICollectionViewFlowLayoutBreakForInvalidSizes来捕获此问题。 单元格高度 --> 42.0
协议
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]?
UICollectionViewDelegate
、UICollectionViewDataSource
和UICollectionViewDelegateFlowLayout
。这些中的最后一个我在类的顶部声明了,没有做更多的操作。 - Declan McKennainsetForSectionsAtIndex
函数。 - Declan McKenna