【发布时间】:2017-08-24 03:37:44
【问题描述】:
我希望使用以下函数,但不幸的是它没有被调用。
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] UICollectionViewFlowLayout 的行为未定义,因为: 2017-03-30 11:25:14.624 Prep[92400:1862856] 项目高度必须小于 UICollectionView 的高度减去部分插入顶部和底部值,减去内容插入顶部和底部值。 2017-03-30 11:25:14.624 Prep[92400:1862856] 相关的 UICollectionViewFlowLayout 实例是 , 并附加到 ;层 = ;内容偏移:{0, 0}; contentSize: {0, 68.5}> 集合视图布局: . 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]?
【问题讨论】:
-
尝试给集合视图一个恒定的高度并测试它是否工作。也许是高度限制造成了问题。
-
@Kingalione 这消除了控制台错误(我使用的是比例高度约束)但仍然没有调用该函数。
-
你实现了正确的委托吗?
-
@Kingalione 我已经用使用的协议更新了这个问题。我使用了
UICollectionViewDelegate、UICollectionViewDataSource和UICollectionViewDelegateFlowLayout。我在班级顶部声明的最后一个,仅此而已。 -
是的,它是正确的协议。那么 cellForItemAt 和 numberOfItemsInSection 是否被调用?