[英]Swift prefersLargeTitles not working if there is a HeaderView in TableView
[英]IOS swift tableview get headerview for a section
以下是我将标题添加到部分的代码
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let optionsForSection = isMutliLevelOptions() ? options : optionsList?[section]
let sectionView = MenuOptionHeaderView()
sectionView.isMultiSelectAllowed = allowsMultipleSelection(section)
sectionView.selectedOptionsCount = getSelectedCount(section)
sectionView.option = optionsForSection
sectionView.setUI()
//headerView = sectionView
return sectionView
}
以下是访问部分标题的代码
func updateHeader(section : Int) {
let headerView : MenuOptionHeaderView = optionsTableView.headerView(forSection: section) as! MenuOptionHeaderView
headerView.selectedOptionsCount = getSelectedCount(section)
headerView.setUI()
}
但是上面的代码崩溃了,说Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
我也收到了来自“UITableViewHeaderFooterView?”的警告。 对于我的代码,不相关的类型“MenuOptionHeaderView”总是失败
我正在使用 .xib 文件创建 MenuOptionHeaderView 并且工作正常我想知道如何获取特定部分的标题
以下是代码 MenuOptionHeaderView
import UIKit
class MenuOptionHeaderView: UIView {
@IBOutlet weak var optionNameLbl: UILabel!
@IBOutlet weak var selectMinLbl: UILabel!
@IBOutlet var contentView: UIView!
var option : Options?
var selectedOptionsCount = 0
var isMultiSelectAllowed = false
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("MenuOptionHeaderView", owner: self, options: nil)
contentView.fixInView(self)
}
func setUI() {
optionNameLbl.text = isMultiSelectAllowed ? "\(option?.name ?? "")(\(selectedOptionsCount))" : (option?.name ?? "")
}
}
您的视图MenuOptionHeaderView
必须从UITableViewHeaderFooterView
继承,否则此调用:
optionsTableView.headerView(forSection: section) as! MenuOptionHeaderView
不会工作,总是返回零。
UITableViewHeaderFooterView
继承您的自定义标题视图。class MenuOptionHeaderView: UITableViewHeaderFooterView {
....
static var nib:UINib {
return UINib(nibName: identifier, bundle: ZohoSearchKit.frameworkBundle)
}
static var identifier: String {
return String(describing: self)
}
}
使用dequeueReusableHeaderFooterView
,这样就不会每次都创建您的标题视图。 它将提高您的滚动性能。
2.1 在 UITableview 中注册您的标题视图。
optionsTableView.register(MenuOptionHeaderView.nib,forHeaderFooterViewReuseIdentifier: MenuOptionHeaderView.identifier)
2.2 复用headerview
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: MenuOptionHeaderView.identifier) as? MenuOptionHeaderView { .... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.