我有一个要向其添加部分的 UITableView。我创建了一个单独的类,它是 UITableHeaderFooterView 的子类。这是它的代码:
import UIKit
class SectionHeader: UITableViewHeaderFooterView {
@IBOutlet weak var sectionHeaderLabel: UILabel!
@IBOutlet weak var sectionHeaderButton: UIButton!
@IBAction func showHideViewAction(sender: UIButton) {
}
}
我想要做的是在包含我的 UITableView 的 ViewController 中设置此类中包含的 UILabel 属性。在该类的 viewDidLoad 方法中,我按如下方式注册 Nib :
let nib = UINib(nibName: "SectionHeaderView", bundle: nil)
self.addTravellerTableView.registerNib(nib, forHeaderFooterViewReuseIdentifier: "SectionHeaderView")
我现在尝试在以下方法中设置 UILabel 属性的文本:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 1 {
let headerView = self.addTravellerTableView.dequeueReusableHeaderFooterViewWithIdentifier("SectionHeaderView")
//headerView.sectionHeaderLabel doesn't work.
return headerView
} else if section == 2{
return nil
} else {
return nil
}
}
我无法调用 UILabel 属性 sectionHeaderLabel。在单步执行代码时,“headerView”至少作为 UIView 类型的对象出现。
谁能看出我做错了什么?
最佳答案
您需要告诉编译器函数调用将返回的类的具体类型,因此您需要添加 as! SectionHeader
到您使 View 出队的行的末尾。
关于ios - 尝试使用 Swift 在 UITableView 的节标题的 UIView 内设置 UILabel 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820841/