我在兩個部分之間創建了一個間隙,我不知道如何擺脫它。
我有4個部分:
- 第 1 節(生命串列),
- 2(黎明合唱),和
- 3(搜索)
- 4(鳥類)
第 1、2 和 3 節都有(相同型別)標題,但第 4 節沒有。第 1、2 和 4 節有頁腳,但第 3 節沒有。
在應用加載時,第 3 節和第 4 節之間會產生一個空白,其中沒有頁眉和頁腳。我試圖通過以下代碼來防止這種情況:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerIdentifier = HomeViewSectionHeaderView.reuseIdentifier
guard let view = tableView.dequeueReusableHeaderFooterView(
withIdentifier: headerIdentifier)
as? HomeViewSectionHeaderView
else {
return nil
}
if !sections[section].showSectionHeader {
return nil
}
view.textLabel?.text = sections[section].title
return view
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard let view = tableView.dequeueReusableHeaderFooterView(
withIdentifier: HomeViewSectionFooterView.reuseIdentifier)
as? HomeViewSectionFooterView
else {
return nil
}
if !sections[section].showSectionFooter {
return nil
}
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (sections[section].showSectionHeader){
return 50.0
}
return 0.0
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
if (sections[section].showSectionHeader){
return 50.0
}
return 0.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if (sections[section].showSectionFooter){
return 8.0
}
return 0.0
}
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
if (sections[section].showSectionFooter){
return 8.0
}
return 0.0
}
在視圖層次結構上我們可以清楚地看到它是一個沒有任何元素的間隙:

全屏看起來是這樣的:

uj5u.com熱心網友回復:
而不是回傳的0到 heightForFooterInSection并heightForHeaderInSection試圖回傳一個非零值(接近于當然0),如0.001或.leastNormalMagnitude或.leastNonzeroMagnitude
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if (sections[section].showSectionFooter){
return 8.0
}
return .leastNormalMagnitude //or you can use .leastNonzeroMagnitude or return a non zero value like 0.001
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (sections[section].showSectionHeader){
return 50.0
}
return .leastNormalMagnitude //or you can use .leastNonzeroMagnitude or return a non zero value like 0.001
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/355772.html
