简体   繁体   English

迅速4无法将类型UIView的值转换为UITableViewHeaderFooterView

[英]swift 4 Could not cast value of type UIView to UITableViewHeaderFooterView

I have a class for a table view declared as: 我有一个表视图的类声明为:

class boatsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

In this class, I want to change the background color and the text color for the sections. 在本课程中,我想更改各节的背景颜色和文本颜色。 To this end, I have implemented the following two classes: 为此,我实现了以下两个类:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = Constants.ppsBlue
    return view
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}

When I run the code, I get a Thread 1: signal SIGABRT on the 运行代码时,我在线程1上收到信号SIGABRT:

let header = view as! UITableViewHeaderFooterView

line of the willDispayHeaderView function. willDispayHeaderView函数的代码行。 If I comment out all of the code in both of these functions, the app runs as it should and I get the section title displaying correctly. 如果我注释掉这两个函数中的所有代码,则该应用程序将按预期运行,并且我将正确显示节标题。 But it isn't in the colors which I'd like to be using. 但这不是我想要使用的颜色。 All I'm trying to do is change the background color for the section and change the text color. 我要做的就是更改该部分的背景颜色并更改文本颜色。 If I comment out the code in the willDisplayHeaderView function, the app works, the section background color is the color I want, but I do not see the title of the section. 如果我将willDisplayHeaderView函数中的代码注释掉,则该应用程序可以正常工作,该部分的背景颜色是我想要的颜色,但是看不到该部分的标题。

Any ideas? 有任何想法吗?

Ok, here's what I did to fix this problem. 好的,这是我为解决此问题所做的工作。 I created a new class of type UITableViewHeaderFooterView: 我创建了一个类型为UITableViewHeaderFooterView的新类:

protocol BoatHeaderViewDelegate {
func toggleSection(header: BoatHeaderView, section: Int)
}

class BoatHeaderView: UITableViewHeaderFooterView {

    var delegate: BoatHeaderViewDelegate?
    var section: Int!

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderAction)))
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    @objc func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer) {
        let cell = gestureRecognizer.view as! BoatHeaderView
        delegate?.toggleSection(header: self, section: cell.section)
    }

    func customInit(title: String, section: Int, delegate: BoatHeaderViewDelegate) {
        self.textLabel?.text = title
        self.section = section
        self.delegate = delegate
    }


    override func layoutSubviews() {
        super.layoutSubviews()
        self.textLabel?.textColor = UIColor.white
        self.contentView.backgroundColor = Constants.ppsBlue
    }
}

Then, in my boatsVC class: 然后,在我的boatVC类中:
1) I added the BoatHeaderViewDelegate to the class definition 1)我将BoatHeaderViewDelegate添加到类定义
2) I created a toggleSection 2)我创建了一个toggleSection
3) I removed the willDisplayHeaderView function 3)我删除了willDisplayHeaderView函数
4) I changed the viewForHeaderInSection as follows: 4)我将viewForHeaderInSection更改如下:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = BoatHeaderView()
    header.customInit(title: thisRaceName, section: 1, delegate: self)
    return header
}

Now, when I run the app, the background color and the text color for the section is what I need it to be. 现在,当我运行该应用程序时,该部分的背景颜色和文本颜色就是我需要的颜色。

You can achieve it simply through a single function by, 您可以通过一个功能简单地实现它,

 func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){

    view.tintColor = UIColor.clear
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.red
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM