簡體   English   中英

自定義標題有 nil 插座

[英]Custom header has nil outlets

我將以下代碼用於我的自定義標題,並且單元格中的對象為零。 我試圖重新連接插座並使用 uiview 而不是 tableviewcell,但沒有任何改變。 該單元格位於 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
    }

視圖層次: 在此處輸入圖片說明 ]

連接的對象: 在此處輸入圖片說明

在 viewForHeaderInSection 中引用時為零: 在此處輸入圖片說明

在我的項目中我沒有添加這行代碼:

self.tableView.register(CaptionHeaderCell.self, forHeaderFooterViewReuseIdentifier: "CaptionHeaderCell")

我直接在以下位置創建了單元格:

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

這也不是HeaderFooterview

我在下面的代碼行中添加了它,它起作用了:

var headerView: CaptionHeaderCell? = tableView.dequeueReusableCell(withIdentifier: "CaptionHeaderCell") as? CaptionHeaderCell

嘗試它是否適用於您的場景。

當我使用此代碼時:

func register<T: UITableViewHeaderFooterView>(_ classType: T.Type) {
    let desc = String(describing: classType)
    register(classType, forHeaderFooterViewReuseIdentifier: desc)
}

我也得到了零引用,但如果更改為:

func register<T: UITableViewHeaderFooterView>(_ classType: T.Type) {
    let desc = String(describing: classType)
    let nib = UINib(nibName: desc, bundle: nil)
    register(nib, forHeaderFooterViewReuseIdentifier: desc)
}

一切都很好:)

對我來說沒有任何效果,解決方案是使用 UITableViewCell 而不是 UITableViewHeaderFooterView 我的代碼或多或少像這樣..

-組件-

class TitleHeaderDetails: UITableViewCell{

// MARK: - Outlets
@IBOutlet weak var title: UILabel!
@IBOutlet weak var imgLine: UIImageView!

// MARK: - Lifeecycle
override func awakeFromNib() {
    super.awakeFromNib()
    
}

}

-在 viewDidLoad -

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

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

-在委托中-

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
}

-我的結果- 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM