简体   繁体   中英

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. The cell is located inside a tableview that is in a uiviewcontroller.

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
    }

Hierarchy of view: 在此处输入图片说明 ]

Objects connected: 在此处输入图片说明

Nil when referenced in 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 .

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

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

registerCell(tableView: tableView, cellType: TitleHeaderDetails.self)

- in RegisterCell - add in your class

 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 -

 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
}

- My Result - 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM