2017-10-07 77 views
0

我有一個擁有兩個自定義單元的UICollection視圖。 UICollectionViewCell自定義單元使用NIB構建。在其中一個UICollectionViewCell NIB中,我有一個UITableView。但是,UITableView本身具有自定義單元格。所以我的問題是:如何使用自定義單元格加載已經是NIB的UICollectionViewCell中的UIT

如何加載UITableView自定義單元而不是UICollectionViewCell NIB?

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 
    } 
} 

回答

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

在幾乎所有情況下,XIB_File_Name == XIB_Class_Name

+0

感謝您的答覆@Roy。這工作。 –

+0

嘿@PaulS。我在返回'cellForItem'時使用了我的答案,但不幸的是它崩潰了,即使使用'let cell = UINib(nibName:「XIB_File_Name」,bundle:nil).instantiate(withOwner:nil,options:nil)[0] as! XIB_Class_Name'的結果是一樣的,那麼我的答案是如何爲你工作的? – Roy

+0

這是我的代碼'let cell = Bundle.main.loadNibNamed(「ShotInformationTableViewCell」,owner:self,options:nil)? ShotInformationTableViewCell'工作正常,沒有崩潰。 –

相關問題