IOS; программный collectionView с пользовательскими заголовками
Я делаю приложение iOS быстрым, и я пытаюсь сделать collectionView программным путем.
Я хочу использовать свой собственный подкласс UICollectionReusableView в качестве заголовка для коллекции, потому что мне нужны кнопки и растяжимое изображение в заголовке.
SupView - это UICollectionReusableView.
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200))
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell") // UICollectionReusableView
self.view.addSubview(collectionView)
}
Я пытаюсь вставить дополнительный вид в viewForSupplementaryElementOfKind
, как это, но я получаю сообщение об ошибке при попытке создать заголовок:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var reusableView : UICollectionReusableView? = nil
// Create header
if (kind == UICollectionElementKindSectionHeader) {
// Create Header
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView
headerView.frame = CGRectMake(0, 0, view.frame.width, 200)
reusableView = headerView
}
return reusableView!
}
Ошибка находится в let headerView = ...
и говорит: "сигнал SIGABRT"
Как мне инициализировать headerview, поэтому я могу вносить свой вклад в мой поток?, возможно, с помощью
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
но если я попытаюсь зарегистрировать класс SupView, это даст мне ошибку:
.../collectionViewPlay/ViewController.swift: 32: 24: Невозможно вызвать 'registerClass' со списком аргументов типа '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'
Любые идеи?
EDIT:
Была запрошена реализация подкласса:
import UIKit
class SupView: UICollectionReusableView {
//////////////////////////////////////////////////////////////////////////////
override init(frame: CGRect) {
super.init(frame: frame)
self.myCustomInit()
}
//////////////////////////////////////////////////////////////////////////////
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.myCustomInit()
}
func myCustomInit() {
print("hello there from SupView")
}
}
Ответы
Ответ 1
Так что я понял это, вдохновившись Мохамадом Фархандом.
Проблема заключалась в том, что мне пришлось зарегистрировать сам подкласс с помощью collectionView, а не UICollectionReusableView.self
, я использовал экземпляр подкласса someView
. Так что это решило мою проблему:
collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")
И как инициализировать вид:
someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView
Ответ 2
Обратите внимание, что Swift 4.1 переименовывает константу ofKind: в UICollectionView.elementKindSectionHeader
.
Ответ 3
вы можете сделать это следующим образом:
// Setup Header
self.collectionView?.registerClass(CollectionCustomHeader.self, forSupplementaryViewOfKind: CustomeHeaderHeader, withReuseIdentifier: "customHeader")
и
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == CustomeHeaderHeader {
let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "parallaxHeader", forIndexPath: indexPath)
return view
}
Ответ 4
Вот ответ Swift 3 & 4, который я использовал в проекте
self.collectionView.register(LibraryHeaderNib.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "LibraryHeaderNib")
и внутри viewForSupplementaryElementOfKind
let reusableView = self.collectionView!.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "LibraryHeaderNib", for: indexPath) as! LibraryHeaderNib
return reusableView