2015-10-06 89 views
1

更新到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 
} 

而作为在标题中说我得到以下警告:

从“的UITableViewCell”无关型“的UIView”

演员总是失败

我不明白为什么会失败,因为UITableViewCell的是子类的UIView,那么演员应该没问题。然而迅速编译器并不这么认为:)

+1

我想这没有必要将UIView子类强制转换为UIView。 – kirander

+1

你想从UITableViewCell施放?到UIView,这确实总是会失败。你可以投向UIView? - 那么它总会成功。所以最好的选择就是不要投 - 所有事情都会按照你的预期工作。 –

回答

1

你不应该施放它。

这应该足以

let result = tableView.dequeueReusableCellWithIdentifier("CustomHeader") 

,其中结果是UITableViewCell?

如果你有一个自定义的UITableViewCell,姑且称之为customTableViewCell,你可以做到以下几点:

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

是的,我试图上升 - 你有没有觉得愚蠢,因为我现在做:) –

0

你并不需要强制转换的UITableViewCell到UIView的,因为它总是会成功的,找你得到上面的错误,因为你投可选到UIView的

result = tableView.dequeueReusableCellWithIdentifier("CustomHeader")! as? UIView 

这将工作,但没有用它来铸造它。

+0

这样我会得到另一个错误,该错误将始终成功。见@威廉的答案是正确的。 –

+0

是的,我在上面提到过。它会一直成功,所以你不需要铸造 – Lukas