ios - 尝试使用 Swift 在 UITableView 的节标题的 UIView 内设置 UILabel 的文本

标签 ios swift uitableview uiview

我有一个要向其添加部分的 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/

相关文章:

ios - 授予某些 ViewController 访问功能的最有效方法

ios - 查询 CloudKit 用户记录给出 "Can' t 查询系统类型”

objective-c - 劫持或阻止 UITableView didSelectRowAtIndexPath

swift - (Swift) 如何更改单元格外的标签文本(事件发生的地方)?

swift - 在uitableview swift中同时添加两行

ios - Fabric 数据与 iTunes Connect 数据不同

ios - 使用 Swift 1.2 从 Parse 检索对象

iOS:当 SignaturePadView 在 UIScrollView 中时,如何防止滚动?

ios - Swift UICollectionView 水平滚动不起作用

ios - 为什么 AppDelegate.swift 窗口是可选的?