2016-09-21 64 views
1

我有一个可以接受泛型类型的自定义单元。Swift 3 Generic不能转换UITableViewCell类型的值

class MyCell<T>: UITableViewCell where T: MyCellDataSource, T: MyCellDelegate 

如何使用它,或者在转出单元时输入或推断它?

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
+0

你可以做'让电池= tableView.dequeueReusableCell(withIdentifier: “细胞”,为:indexPath)为! MyCell'我认为 – Eric

+0

不行,那不行。这是通常的方式,我试过了。 – Axel

+1

您不能转换为未指定的泛型类型,只能指定其具体版本,即指定了泛型'T'。沿着'let cell = tableView.dequeueReusableCell(withIdentifier:“cell”,for:indexPath)行的东西就像!了myCell '。 – dfri

回答

0

在Swift 3.0 我最近使用了相同的实现。 我通过以下代码解决了这个问题。

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCell<TName> 
//Configure cell here 
return cell! 

希望它可以帮助

相关问题