簡體   English   中英

實現 tableView:heightForFooterInSection 時,UITableView 的頁腳不與表格一起滾動:

[英]UITableView's footer is not scrolling along with the table when implemented tableView:heightForFooterInSection:

這是tableView:heightForFooterInSection:未實現時的外觀:

在此處輸入圖片說明

一旦我將此行添加到代碼中:

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 120
}

發生以下事情:

  1. 當我開始滾動到底部時

在此處輸入圖片說明

  1. 當我滾動到底部時

在此處輸入圖片說明

為什么屏幕底部固定有一個灰色空間? 它不應該是那樣的。 怎么了?

您使用tableView.tableFooterView設置的頁腳與部分頁腳不同。

每個部分都可以有一個頁腳,其高度由tableView:heightForFooterInSection:指定,並且 tableView 本身可以有一個頁腳(在所有部分下方)。 這個頁腳是使用tableView.tableFooterView設置的,您只需通過設置視圖的框架即可更改其高度。

只需將以下代碼添加到管理 tableview 的控制器的 viewDidLoad 中:

override func viewDidLoad() {
super.viewDidLoad()

//...

  // Remove extra separators
  tableView.tableFooterView = UIView(frame: CGRectZero)
}

對於類似的情況,我使用了 tableFooterView。

final class TableViewWithDynamicFooter: UITableView {
 private var isAwaitnigReloading: Bool = false
 private var lastCalculatedFooterSize: CGFloat?
 
 private func setupHeaderHeight() {
    guard let footer = self.tableFooterView else { return }
    
    let originContentHeight = self.contentSize.height - (lastCalculatedFooterSize ?? 0)
    let emptySpaceUnderContent = self.bounds.height - originContentHeight
    let minFooterHeight = footer.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
    assert(minFooterHeight != 0, "Footer height constraints don't let usage of systemLayoutSizeFitting")
  
    footer.frame.size.height  = max(emptySpaceUnderContent, minFooterHeight)
    self.contentSize.height = footer.frame.origin.y + footer.frame.height
    lastCalculatedFooterSize = footer.frame.size.height
  
    isScrollEnabled = footer.frame.size.height == minFooterHeight
 }
 
 override func reloadData() {
    isAwaitnigReloading = true
    super.reloadData()
 }
 
override func layoutSubviews() {
  super.layoutSubviews()
 
 guard isAwaitnigReloading == true else { return } // allow to calculate footer size once, after reload regardless of the number of layoutSubviews calls
 setupHeaderHeight()
 isAwaitnigReloading = false
}
}

我的實現演示在這里: https : //github.com/ArturRuZ/TableViewWithDynamicFooter

暫無
暫無

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

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