ios - 从 "UITableViewCell"转换为不相关类型 "UIView"总是失败 iOS9

标签 ios swift casting ios9

更新到 iOS9 后,我开始在以下代码中看到一个奇怪的警告:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    var result: UIView?

    if UserPerspective.List == currentUser.perspective.value
    {
        result = tableView.dequeueReusableCellWithIdentifier("CustomHeader") as? UIView
    }

    return result
}

正如标题中所述,我收到以下警告:

Cast from "UITableViewCell" to unrelated type "UIView" always fails

我不明白为什么它失败,因为 UITableViewCell 是 UIView 的子类,那么强制转换应该没问题。然而 swift 编译器并不这么认为:)

最佳答案

你不应该转换它。

这应该足够了

let result = tableView.dequeueReusableCellWithIdentifier("CustomHeader")

结果是UITableViewCell?

如果您有自定义 UITableViewCell,我们将其称为 customTableViewCell,您可以执行以下操作:

let result = tableView.dequeueReusableCellWithIdentifier("CustomHeader") as! customTableViewCell

关于ios - 从 "UITableViewCell"转换为不相关类型 "UIView"总是失败 iOS9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32966010/

相关文章:

postgresql - Postgres @sqlupdate 转换数据类型

c++ - cout << 和 cast c++ 的歧义

java - 类型转换与文字赋值

ios - 如何保留 CAMetalLayer 的支持?

ios - SpriteKit 一次防止多个触摸

swift - tvOS/swift 3 : How fix broken UICollectionView selection animation?

ios - Facebook 登录 : iOS/Swift - Click button twice

ios - 从字符串数组设置 UITable 中的详细文本标签

iphone - MKMapView setRegion animated 不显示动画

ios - 防止进入静态库代码