2016-09-21 53 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! 

希望它可以幫助

相關問題