[英]Custom header has nil outlets
I am using the following code for my custom header and the objects within the cell are nil.我将以下代码用于我的自定义标题,并且单元格中的对象为零。 I have tried to reconnect the outlets and use a uiview instead of a tableviewcell, but nothing changed.我试图重新连接插座并使用 uiview 而不是 tableviewcell,但没有任何改变。 The cell is located inside a tableview that is in a uiviewcontroller.该单元格位于 uiviewcontroller 中的 tableview 内。
class CaptionHeaderCell: UITableViewHeaderFooterView {
@IBOutlet weak var typeField: UILabel!
@IBOutlet weak var nameField: UILabel!
@IBOutlet weak var statusField: UILabel!
@IBOutlet weak var timeField: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
override func viewDidLoad() {
super.viewDidLoad()
//self.tableView.register(UINib(nibName: "CaptionHeaderCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "CaptionHeaderCell")
self.tableView.register(CaptionHeaderCell.self, forHeaderFooterViewReuseIdentifier: "CaptionHeaderCell")
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CaptionHeaderCell") as! CaptionHeaderCell
//Crashes when trying to set text
header.timeField.text = self.getCallTime(startTime: call!.startTime, endTime: call!.endTime)
return header
}
Nil when referenced in viewForHeaderInSection:在 viewForHeaderInSection 中引用时为零:
In my project I haven't added this line of code:在我的项目中我没有添加这行代码:
self.tableView.register(CaptionHeaderCell.self, forHeaderFooterViewReuseIdentifier: "CaptionHeaderCell")
I have created the cell directly in :我直接在以下位置创建了单元格:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
and that too not as a HeaderFooterview
.这也不是HeaderFooterview
。
I added below line of code and it worked:我在下面的代码行中添加了它,它起作用了:
var headerView: CaptionHeaderCell? = tableView.dequeueReusableCell(withIdentifier: "CaptionHeaderCell") as? CaptionHeaderCell
Try if it works in your scenario.尝试它是否适用于您的场景。
When I'm using this code:当我使用此代码时:
func register<T: UITableViewHeaderFooterView>(_ classType: T.Type) {
let desc = String(describing: classType)
register(classType, forHeaderFooterViewReuseIdentifier: desc)
}
I got nil references too, but if change to:我也得到了零引用,但如果更改为:
func register<T: UITableViewHeaderFooterView>(_ classType: T.Type) {
let desc = String(describing: classType)
let nib = UINib(nibName: desc, bundle: nil)
register(nib, forHeaderFooterViewReuseIdentifier: desc)
}
Everything is fine :)一切都很好:)
for me nothing worked, the solution was to use UITableViewCell instead of UITableViewHeaderFooterView my code was more or less like this ..对我来说没有任何效果,解决方案是使用 UITableViewCell 而不是 UITableViewHeaderFooterView 我的代码或多或少像这样..
- Component - -组件-
class TitleHeaderDetails: UITableViewCell{
// MARK: - Outlets
@IBOutlet weak var title: UILabel!
@IBOutlet weak var imgLine: UIImageView!
// MARK: - Lifeecycle
override func awakeFromNib() {
super.awakeFromNib()
}
} }
- in viewDidLoad - -在 viewDidLoad -
registerCell(tableView: tableView, cellType: TitleHeaderDetails.self)
- in RegisterCell - add in your class -在 RegisterCell 中- 添加到您的类中
static func registerCellNib<T: UITableViewCell>(_ tableView: UITableView, cellType: T.Type)
public func registerCell(tableView: UITableView, cellType: Any){
YourClass.registerCellNib(tableView, cellType: cellType as! UITableViewCell.Type)
}
- in Delegate - -在委托中-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
- viewForHeaderInSection - - viewForHeaderInSection -
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewForHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
let titleHeader = tableView.dequeueReusableCell(withIdentifier: "TitleHeaderDetails") as! TitleHeaderDetails
titleHeader.frame = viewForHeader.bounds
viewForHeader.addSubview(titleHeader)
return titleHeader
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.