我有一个带有自定义布局的UICollectionView,所以我可以在类似excell的视图中同时滚动水平和垂直。
我有两个自定义CollectionViewCells类的两个原型单元。一个用于内容,一个用于标题,因为我希望它们看起来不同。 我的部分是垂直运行的,每列都是新的部分。我希望每个部分都有一个自定义标题。
目前我有一个有效的细胞名单。我已经为标题实现了以下内容。
CustomCollectionViewController :
[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 :
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
}
问题是细胞永远不会显示出来。调用应该实现头的代码。我对此很陌生并尝试用更困难的练习挑战自己,所以它可能是基本的东西。
未显示错误。该应用程序以正常的excell表开头,没有标题。
任何人都知道我可能做错了什么?
答案 0 :(得分:2)
在Storyboard中构建原型单元格(或标题)时,不应在代码中的collectionView
上注册该类的单元格。这将覆盖您现有的视图,但不会显示。