Ich würde wahrscheinlich meine UICollectionViewCell Unterklasse, definieren alle erforderlichen Layout-Typen enums und übergeben Sie es in meinem CellForItemAtIndexPath als Parameter in einer benutzerdefinierten Methode, so dass die benutzerdefinierte Sammlung View-Zelle selbst zu planen.
Etwas wie folgt aus:
enum MyLayoutType
{
case layoutA
case layoutB
case layoutC
}
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
// MARK: Collection View DataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomCVCell", for: indexPath) as! MyCustomCVCell
// Decide here what layout type you would like to set
cell.setUpWithType(layoutType: .layoutA)
return cell
}
}
Und dann, in der benutzerdefinierten Sammlung Ansicht Zelle Unterklasse (vergessen Sie nicht die benutzerdefinierte Klasse in Ihrer Interface Builder-Datei zuweisen):
class MyCustomCVCell: UICollectionViewCell
{
func setUpWithType(layoutType: MyLayoutType)
{
switch layoutType
{
case .layoutA:
self.backgroundColor = .red
case .layoutB:
self.backgroundColor = .yellow
case .layoutC:
self.backgroundColor = .blue
}
}
}
könnten Sie posten Sie bitte einen Code oder ein Github-Projekt davon? –
Ich habe meine Antwort bearbeitet und ein einfaches Beispiel hinzugefügt. –