iOS Swift 获取某个部分的表格视图头部视图

huangapple go评论89阅读模式
英文:

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

  1. 你应该继承自 UITableViewHeaderFooterView 的自定义表头视图。
class MenuOptionHeaderView: UITableViewHeaderFooterView {
   ....
    
   static var nib: UINib {
      return UINib(nibName: identifier, bundle: ZohoSearchKit.frameworkBundle)
   }
 
   static var identifier: String {
        return String(describing: self)
   }
}
  1. 使用 dequeueReusableHeaderFooterView,这样你的表头视图不会在每次创建,可以提高滚动性能。

    2.1 在 UITableView 中注册你的表头视图。

optionsTableView.register(MenuOptionHeaderView.nib, forHeaderFooterViewReuseIdentifier: MenuOptionHeaderView.identifier)

2.2 重用表头视图。

if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: MenuOptionHeaderView.identifier) as? MenuOptionHeaderView {
   ....
}
英文:
  1. 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)
   }
}
  1. 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 {
     ....
    }

huangapple
  • 本文由 发表于 2020年1月6日 20:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定