Ich bin daran interessiert, eine Collectionview als Teil einer Sammlung anzuzeigen Zelle, aber aus irgendeinem Grund kann nicht herausfinden, wie dies getan werden würde. Wo würde ich die notwendigen Methoden für die cells collectionview implementieren?UICollectionView in einem UICollectionViewCell
Antwort
Es gibt an article that Ash Furrow wrote, die erklärt, wie man einen UICollectionView
innerhalb eines UITableViewCell
setzt. Es ist im Grunde die gleiche Idee, wenn Sie es in einem UICollectionViewCell
verwenden.
Dies ist zu spät für diese Antwort, aber es könnte anderen helfen. Dies ist ein Beispiel für UICollectionView
in einem UICollectionViewCell
.
Beginnen wir mit einem mainCollectionView
. Dann auf jeder Zelle dieser Sammlung erstellen und initialisieren einen neuen UICollectionView
und am richtigen Ort zu tun, ist, dass in diesen folgend Delegierten UICollectionView
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
zB ich die MainCollectionViewCell
hier initialisieren und dann MainCollectionViewCell
nimmt die Logik ein erstellen Hier werden neue UICollectionView
guard let collectionViewCell = cell as? MainCollectionViewCell else { return }
collectionViewCell.delegate = self
let dataProvider = ChildCollectionViewDataSource()
dataProvider.data = data[indexPath.row] as NSArray
let delegate = ChildCollectionViewDelegate()
collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row)
collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
ist der Initialisierer auf MainCollectionViewCell
, die eine neue UICollectionView
func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) {
self.collectionViewDataSource = dataSource
self.collectionViewDelegate = delegate
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier)
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.dataSource = self.collectionViewDataSource
collectionView.delegate = self.collectionViewDelegate
collectionView.tag = row
self.addSubview(collectionView)
self.collectionView = collectionView
collectionView.reloadData()
}
Hoffe, dass hilft !!
Ich habe ein Beispiel dafür gemacht und Github eingefügt. Es zeigt die Verwendung UICollectionView
in einem UICollectionViewCell
.
https://github.com/irfanlone/Collection-View-in-a-collection-view-cell
Alles ist programmatisch getan. Keine Storyboards
Ich habe eine UICollectionView in meiner UICollectionViewCell hinzugefügt. Ich zeige auch, wie man hinzufügen, wieder eine UICollectionViewCell innerhalb des erstellt UICollectionView dieses Ergebnis
import UIKit
class CategoryCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellId = "cell"
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
return collectionView
}()
func setupViews() {
backgroundColor = .blue
addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
addConstrainstWithFormat("H:|-8-[v0]-8-|", views: appsCollectionView)
addConstrainstWithFormat("V:|[v0]|", views: appsCollectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
}
class AppCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = .red
}
}
Mein UICollectionViewController
import UIKit
class FeaturedAppsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView?.backgroundColor = .white
collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(view.frame.width, 150)
}
}
Die ganze Erklärung gefunden werden und wurde von entwickelt wurden, können haben „Let Die Build-App ": https://www.youtube.com/watch?v=Ko9oNhlTwH0&list=PL0dzCUj1L5JEXct3-OV6itP7Kz3tRDmma
- 1. UICollectionViewCell Größe entspricht UICollectionView Größe
- 2. UICollectionView enthält keine UICollectionViewCell in IB
- 3. UICollectionView: UICollectionViewCell-Objekt ist immer null in sizeForItemAtIndexPath
- 4. Verhindern von UICollectionViewCell animierte Darstellung beim Präsentieren von UICollectionView
- 5. Mehrere UICollectionView in einem Controller
- 6. UICollectionViewCell wird nicht angezeigt
- 7. Wie Übergeben der Schwenkgeste an UICollectionView von UICollectionViewCell?
- 8. UICollectionViewCell Schattenfarbe
- 9. UICollectionViewCell Navigation
- 10. Daten nicht in UICollectionviewcell angezeigt?
- 11. Beziehen UICollectionViewCell Aufklebertext
- 12. Wie UICollectionViewCell Größe festlegen?
- 13. UICollectionViewCell zu UIButton Fokus
- 14. UICollectionViewCell Shake
- 15. UIImageView animierte funktioniert nicht richtig in UICollectionViewCell
- 16. Subclassing benutzerdefinierte UICollectionViewCell
- 17. Entwerfen von UICollectionViewCell in Storyboard
- 18. Animiere UICollectionViewCell auf Tap
- 19. iOS Assertionsfehler in UICollectionView
- 20. UICollectionViewCell zu UIButton Fokus in tvOS
- 21. Detect Abschnitt von UICollectionViewCell
- 22. Beschriftung in UICollectionViewCell nicht aktualisiert
- 23. Fehlende Editiereigenschaften und setEditing: animiert: in UICollectionViewCell
- 24. Unterklasse von UICollectionViewCell Nicht angezeigt
- 25. Wie ändert man UICollectionViewCell Höhe?
- 26. get touch location von uicollectionviewcell
- 27. Zellen nicht in UICollectionView angezeigt?
- 28. Zelle von UICollectionView nicht immer nach einem Bildlauf aktualisiert
- 29. Nicht angezeigt (aufgerufen) UICollectionViewCell ... (programmgesteuert)
- 30. Kann UICollectionViewCell innerhalb UITableViewCell