我目前在UITableView中使用了一些自定義節頭視圖。 UITableView加載後會出現視圖,但滾動時會消失。我看到了這個帖子,但似乎已經過時:tableView section headers disappear SWIFT 這裏是我的頭視圖代碼:滾動時UITableView節頭視圖消失
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
switch section {
case 0: break
default:
view.backgroundColor = UIColor.clear
let image = UIImageView(image:"Line")
image.frame = CGRect(x: 8, y: 35, width: 340, height: 1)
view.addSubview(image)
let label = UILabel()
let sectionText = self.sectionTitles[section]
label.text = sectionText
label.textColor = UIColor.white
label.font = UIFont(name:"Helvetica Neue" , size: 17)
label.frame = CGRect(x: 10, y: 8, width: 200, height: 20)
view.addSubview(label)
}
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let headerHeight: CGFloat
switch section {
case 0:
// hide the first section header
headerHeight = CGFloat.leastNonzeroMagnitude
default:
headerHeight = 40
}
return headerHeight
}
可能不相關,但爲什麼你不返回0爲0節頭的高度?爲什麼要返回非常小的值,而不是0呢? – rmaddy
我試過了,它並沒有真正有所作爲 – SwiftyJD
我還建議用更有用的非零初始框架創建標題視圖。 – rmaddy