2017-02-13 12 views
1

У меня есть две ячейки просмотра коллекции A и B, мне нужно загрузить эти ячейки одновременно. Но я не нашел решенийКак я могу загрузить несколько пользовательских представлений в представлении коллекции swift

  firstCollectionView.register(UINib(nibName: "A", bundle: Bundle.main), forCellWithReuseIdentifier: "A") 
    firstCollectionView.register(UINib(nibName: "B", bundle: Bundle.main), forCellWithReuseIdentifier: "B") 

Это два вида и как можно загрузить 2 вида во времени.

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "A", for:indexPath) as? A 
+0

В том же разделе или же ячейку или любое другое условие? –

+0

что вы имеете в виду одновременно? ... вам нужны две разные ячейки или? – John

+1

Вы можете просто отметить как A, так и B в одно и то же время и выбрать, какой из них вы хотите вернуть – Tj3n

ответ

1

Как вы хотите разделить разные типы ячеек? с номером? Например, если raw = 0,2,4,6 и т. Д., У вас будет firstCell, а если raw = 1,3,5 и т. Д., У вас будет secendCell?

Так что, может быть, с чем-то вроде этого:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = UICollectionViewCell() 

     cell = collectionView.register(UINib(nibName: "B", bundle: Bundle.main), forCellWithReuseIdentifier: "B") 

     if indexPath.row % 2 == 0 { 
      cell = collectionView..register(UINib(nibName: "A", bundle: Bundle.main), forCellWithReuseIdentifier: "A") 
     } 

     return cell 
    } 
0

Регистрация CustomCollectionViewCell в viewDidLoad:

var nib1 = UINib(nibName: "CustomCollectionViewCell1", bundle: nil) 
    self.firstCollectionView().registerNib(nib1, forCellReuseIdentifier: "CustomCell1") 
    var nib2 = UINib(nibName: "CustomCollectionViewCell2", bundle: nil) 
    self.firstCollectionView().registerNib(nib2, forCellReuseIdentifier: "CustomCell2") 

Теперь вернуть вашу клетку здесь cellForItemAtIndexPath: метод,

//As per your condition check cell index or section or any other your condition. 


if indexPath.row % 2 == 0 { 

     // Create an instance of CustomCollectionViewCell1 
    var cell: CustomCollectionViewCell1? = tableView.dequeueReusableCell(withIdentifier: "CustomCell1") 
     if self.cell == nil { 
      self.cell = CustomCollectionViewCell1(style: .subtitle, reuseIdentifier: "CustomCell1") 
     } 
    return cell! 

    }else{ 
     // Create an instance of CustomCollectionViewCell2 
    var cell: CustomCollectionViewCell2? = tableView.dequeueReusableCell(withIdentifier: "CustomCell2") 
     if self.cell == nil { 
      self.cell = CustomCollectionViewCell2(style: .subtitle, reuseIdentifier: "CustomCell2") 
     } 
    return cell! 
    }