【发布时间】:2016-01-25 10:44:30
【问题描述】:
我有一个带有自定义布局的 UICollectionView,因此我可以在类似 Excel 的视图中同时水平和垂直滚动。
我有 两个原型单元,其中包含两个自定义 CollectionViewCells 类。一个用于内容,一个用于标题,因为我希望它们看起来不同。 我的部分是垂直运行的,每一列都是一个新部分。我希望每个部分都有一个自定义标题。
目前我有一个有效的细胞名册。我已经为标题实现了以下内容。
CustomCollectionViewController:
- 注册标题单元格(在 viewDidLoad 中)
- 实现 numberOfSectionsInCollectionView、numberOfItemsInSection、cellForItemAtIndexPath
- viewForSupplementaryElementOfKind
r
collectionView?.registerClass(CustomCollectionViewHeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: reuseIdentifierHeader)
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> CustomCollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell
cell.label.text = "S:\(indexPath.section) / i:\(indexPath.item)"
return cell
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let cell = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: reuseIdentifierHeader, forIndexPath: indexPath) as! CustomCollectionViewHeaderCell
cell.backgroundColor = UIColor.redColor()
return cell
}
abort()
}
CustomCollectionViewLayout:
- prepareLayout -> 生成单元格,
- layoutAttributesForItemAtIndexPath -> 返回内容单元格
-
layoutAttributesForSupplementaryViewOfKind -> 返回标题单元格
// Generate cell data if collectionView?.numberOfSections() > 0 { // If collection view has sections for section in 0...(collectionView?.numberOfSections())!-1 { // For every section if collectionView?.numberOfItemsInSection(section) > 0 { // If section have items // Skip item 0 for item in 1...(collectionView?.numberOfItemsInSection(section))!-1{ // For every item // Create individual cell variables let cellIndex = NSIndexPath(forItem: item, inSection: section) let xPos = CGFloat(section) * CELL_WIDTH let yPos = CGFloat(item) * CELL_HEIGHT // Create cell attributes object let cellAttr = UICollectionViewLayoutAttributes(forCellWithIndexPath: cellIndex) cellAttr.frame = CGRectMake(xPos, yPos, CELL_WIDTH, CELL_HEIGHT) // Safe in dictionary cellAttributesDictionary[cellIndex] = cellAttr } } // Generate header cells let cellIndex = NSIndexPath(forItem: 0, inSection: section) let xPos = CGFloat(section) * CELL_WIDTH let yPos = CGFloat(1) * CELL_HEIGHT // Create cell attributes object let cellAttr = UICollectionViewLayoutAttributes(forCellWithIndexPath: cellIndex) cellAttr.frame = CGRectMake(xPos, yPos, CELL_WIDTH, CELL_HEIGHT) cellAttr.zIndex = 1 cellAttr.zIndex = 5 headerCellAttributesDictionary[cellIndex] = cellAttr } } override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let array = cellAttributesDictionary.values // Loop over all cells // Place all cells currently in view in an array var layoutAttrinRect = [UICollectionViewLayoutAttributes]() for cellAttr in array { if(CGRectIntersectsRect(rect, cellAttr.frame)){ layoutAttrinRect.append(cellAttr) } } return layoutAttrinRect}
问题是单元格从未显示。没有调用应该实现标头的代码。我对此很陌生,并尝试通过更困难的练习来挑战自己,所以这可能是一些基本的东西。
没有显示错误。该应用程序以没有标题的普通 Excel 表格开始。
有人知道我做错了什么吗?
【问题讨论】:
-
您是否在 Storyboard 中构建视图?
-
是的,主要是。直到我到达标题。当自定义布局到位时,不可能通过情节提要添加标题。
-
好的,那么让我重新表述我的问题,您是否在情节提要中构建了标题并给它一个重用标识符?
-
是的,我有两个具有不同重用标识符的原型单元。一个用于内容,一个用于标题。
-
你解决过这个问题吗?
标签: swift uicollectionview uicollectionviewlayout