Ios 具有自定义UICollectionViewLayout的UICollectionView中的节标题
我正在使用自定义布局处理Ios 具有自定义UICollectionViewLayout的UICollectionView中的节标题,ios,swift,uicollectionview,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewlayout,我正在使用自定义布局处理UICollectionView。两天后,我无法理解如何将标题添加到UICollectionView。我有一个非常简单的视图控制器(使用自定义布局在故事板中创建): 这是我的自定义布局: class ACollectionViewController: UICollectionViewController { enum Identifiers: String { case CellIdentifier = "Cell" case
UICollectionView
。两天后,我无法理解如何将标题添加到UICollectionView
。我有一个非常简单的视图控制器(使用自定义布局在故事板中创建):
这是我的自定义布局:
class ACollectionViewController: UICollectionViewController {
enum Identifiers: String {
case CellIdentifier = "Cell"
case HeaderIdentifier = "Header"
}
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: Identifiers.HeaderIdentifier.rawValue)
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.CellIdentifier.rawValue, forIndexPath: indexPath) as Cell
cell.backgroundColor = UIColor.redColor()
return cell
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
println("ip = \(indexPath.item)")
var supplementaryView = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView
supplementaryView.backgroundColor = UIColor.blueColor()
return supplementaryView
}
class ALayout: UICollectionViewLayout {
override func prepareLayout() {
super.prepareLayout()
}
override func collectionViewContentSize() -> CGSize {
return self.collectionView!.bounds.size
}
let itemWidth = 40
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
let x: Float = Float(10) * Float(indexPath.item)
let y: Float = Float(indexPath.item * itemWidth)
attributes.frame = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(itemWidth), height: CGFloat(itemWidth))
return attributes
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
var attributes = [UICollectionViewLayoutAttributes]()
let sectionsCount = self.collectionView!.dataSource!.numberOfSectionsInCollectionView!(self.collectionView!)
for section in 0..<sectionsCount {
/// add header
attributes.append(self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: NSIndexPath(forItem: 0, inSection: section)))
let itemsCount = self.collectionView!.numberOfItemsInSection(section)
for item in 0..<itemsCount {
let indexPath = NSIndexPath(forItem: item, inSection: section)
attributes.append(self.layoutAttributesForItemAtIndexPath(indexPath))
}
}
return attributes
}
override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
var attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, withIndexPath: indexPath)
attributes.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
return attributes
}
}
我理解,这是因为单元格和我要添加的节头具有相同的indexPath,但头的indexPath应该是什么?或者我做的完全错了
提前感谢您。由于您正在使用
var supplementaryView = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView
尝试将页眉和页脚出列(在collectionView:viewforsupplementalelementofkind
中)。改成
var supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier:Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView
应该消除崩溃,这样您就可以调查indexath了。但在我的测试中,indexPath总是仅为“0”。而不是
var supplementaryView = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView
应该是
let supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(kind withReuseIdentifier:Identifiers.HeaderIdentifier.rawValue, forIndexPath:indexPath)
您对标题使用了错误的函数。根据apple文档,页眉和页脚使用以下内容:
func dequeueReusableSupplementaryViewOfKind(_ elementKind: String,
withReuseIdentifier identifier: String,
forIndexPath indexPath: NSIndexPath!) -> AnyObject
而不是:
var supplementaryView = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView
var supplementaryView = collectionView.dequeueReusableCellWithReuseIdentifier(Identifiers.HeaderIdentifier.rawValue, forIndexPath: indexPath) as UICollectionReusableView