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
}
}
感謝您的答覆@Roy。這工作。 –
嘿@PaulS。我在返回'cellForItem'時使用了我的答案,但不幸的是它崩潰了,即使使用'let cell = UINib(nibName:「XIB_File_Name」,bundle:nil).instantiate(withOwner:nil,options:nil)[0] as! XIB_Class_Name'的結果是一樣的,那麼我的答案是如何爲你工作的? – Roy
這是我的代碼'let cell = Bundle.main.loadNibNamed(「ShotInformationTableViewCell」,owner:self,options:nil)? ShotInformationTableViewCell'工作正常,沒有崩潰。 –