簡體   English   中英

heightForHeaderInSection 只調用一次

[英]heightForHeaderInSection only called once

Swift 中的編碼 3. 有一個帶有自定義單元格和標題的 tableView。

我有一個帶有自定義單元格和標題的 tableView。 標題中有兩 (2) 個標簽,並且具有動態單元格高度,因為標簽可能很長。 我的問題是第一次配置 tableView 和部分時,標簽顯示為它應該的樣子,但是,在向下滾動然后備份標題的布局后,以某種方式中斷了。

當 tableView 首次加載並且用戶向下滾動時,一切看起來都很好

正如你在下面看到的,在我向下滾動然后回到單元格后,標簽被截斷。

向下滾動后備份標簽被切斷

在打印出正在調用的方法后,我發現第一次向下滾動 tableView 時,會調用以下兩 (2) 個覆蓋函數。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    print("\(section) heightForHeaderInSection")
    print("\(section) returning AUTO for header")
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    print("\(section) viewForHeaderInSection")
    let header = tableView.dequeueReusableCell(withIdentifier: "QuestionHeader") as! QuestionHeader
    header.delegate = self
    header.contentView.backgroundColor = UIColor.groupTableViewBackground
    header.questionTextLabel.text = String(questionStringArray[section])
    header.questionNumberLabel.text = (String(section + 1) + ")")
    return header.contentView
}

但是當我向上滾動時,只調用 viewForHeader 函數,我認為因為高度不再設置為 UITableViewAutomaticDimension 標簽被截斷?

有任何想法嗎?

您應該返回header ,而不是header.contentViewtableView: viewForHeaderInSection:方法:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  let header = tableView.dequeueReusableCell(...
  ...
  return header
}

暫無
暫無

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

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