如何在已经是 NIB 的 UICollectionViewCell 中加载带有自定义单元格的 UITable
How to load a UITable with custom cell inside a UICollectionViewCell that is already a NIB
我有一个包含两个自定义单元格的 UICollection 视图。 UICollectionViewCell 自定义单元格是使用 NIB 构建的。在其中一个 UICollectionViewCell NIB 中,我有一个 UITableView。但是,UITableView 本身具有自定义单元格。所以我的问题是:
如何为 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
}
}
let cell = Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name
几乎在所有情况下,XIB_File_Name == XIB_Class_Name
我有一个包含两个自定义单元格的 UICollection 视图。 UICollectionViewCell 自定义单元格是使用 NIB 构建的。在其中一个 UICollectionViewCell NIB 中,我有一个 UITableView。但是,UITableView 本身具有自定义单元格。所以我的问题是:
如何为 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
}
}
let cell = Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name
几乎在所有情况下,XIB_File_Name == XIB_Class_Name