英文:
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 ?? "")
}
}
英文:
Following is my code to add header to 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
}
Following is code to access header of a section
func updateHeader(section : Int) {
let headerView : MenuOptionHeaderView = optionsTableView.headerView(forSection: section) as! MenuOptionHeaderView
headerView.selectedOptionsCount = getSelectedCount(section)
headerView.setUI()
}
But above code is crashing saying Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Also I am getting warning Cast from 'UITableViewHeaderFooterView?' to unrelated type 'MenuOptionHeaderView' always fails at for my code
I am creating MenuOptionHeaderView using .xib file and is working fine I want to know how can I get header for a particular section
Following is code 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 ?? "")
}
}
答案1
得分: 5
你的视图 MenuOptionHeaderView
必须继承自 UITableViewHeaderFooterView
,否则这个调用:
optionsTableView.headerView(forSection: section) as! MenuOptionHeaderView
将无法工作并始终返回 nil。
来源:https://stackoverflow.com/questions/20882561/how-to-get-section-header-from-uitableview
英文:
your view MenuOptionHeaderView
must inherit from UITableViewHeaderFooterView
or else this call :
optionsTableView.headerView(forSection: section) as! MenuOptionHeaderView
will not work and always returns nil.
Source : https://stackoverflow.com/questions/20882561/how-to-get-section-header-from-uitableview
答案2
得分: 0
- 你应该继承自
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 重用表头视图。
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: MenuOptionHeaderView.identifier) as? MenuOptionHeaderView {
....
}
英文:
- You should inherit your custom header view from
UITableViewHeaderFooterView
.
class MenuOptionHeaderView: UITableViewHeaderFooterView {
....
static var nib:UINib {
return UINib(nibName: identifier, bundle: ZohoSearchKit.frameworkBundle)
}
static var identifier: String {
return String(describing: self)
}
}
- Use
dequeueReusableHeaderFooterView
, So, that your header view won't be created every time. it will improve your scrolling performance.
2.1 register your header view in UITableview.
optionsTableView.register(MenuOptionHeaderView.nib,forHeaderFooterViewReuseIdentifier: MenuOptionHeaderView.identifier)
2.2 Reuse headerview
if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: MenuOptionHeaderView.identifier) as? MenuOptionHeaderView {
....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论