2017-10-07 4 views
0

J'ai une vue UICollection qui a deux cellules personnalisées. Les cellules personnalisées UICollectionViewCell sont construites à l'aide de NIB. À l'intérieur d'un NIB UICollectionViewCell, j'ai un UITableView. Cependant, UITableView lui-même a des cellules personnalisées. Donc ma question est:Comment charger un UITable avec une cellule personnalisée dans un UICollectionViewCell qui est déjà un NIB

Comment charger une cellule personnalisée pour UITableView à la place d'une NIB UICollectionViewCell?

import UIKit 

class HeaderFooterCollectionViewCell: UICollectionViewCell { 

    //Tableview 
    @IBOutlet weak var tableView: UITableView! 

    //Buttons 
    @IBOutlet weak var resetButton: UIButton! 
    @IBOutlet weak var periodButton: UIButton! 
    @IBOutlet weak var undoButton: UIButton! 


    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 

     // Register cell classes 
     tableView.register(UINib(nibName: "ShotInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "cell") 
    } 

} 

extension HeaderFooterCollectionViewCell: UITableViewDataSource { 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShotInformationTableViewCell 

     return cell 
    } 
} 

Répondre

1
let cell = Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name 

Dans presque tous les cas, XIB_File_Name == XIB_Class_Name

+0

Merci pour la réponse @Roy. Ça a marché. –

+0

Hey @PaulS. J'utilise ma réponse en retournant 'cellForItem' mais malheureusement il s'est écrasé, même si j'utilise' let cell = UINib (nibName: "XIB_File_Name", bundle: nil) .instantiate (avecOwner: nil, options: nil) [0] as! XIB_Class_Name' le résultat est le même, alors comment ma réponse a-t-elle fonctionné pour vous? – Roy

+0

Ceci est mon code 'let cell = Bundle.main.loadNibNamed (" ShotInformationTableViewCell ", propriétaire: self, options: nil) ?. ShotInformationTableViewCell fonctionne correctement sans panne. –