2017-07-19 92 views
-1

我想創建細胞,我知道的對象,相順應的具體協議。創建符合協議斯威夫特

然而,當我嘗試做:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell<ModelBinding> = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath) 

     return cell 
    } 

我得到了一個錯誤。如何解決它?

+0

你CA n請擴展到所有的tableview細胞或特定細胞類,並符合該協議 –

+0

@Lu_我真的相信,有一個更簡單的方法 –

+0

這是extremally簡單的方法,你要如何做任何事情比這更容易,它符合或者不是,那簡單 –

回答

2

你必須繼承細胞,它證實您要使用的協議。在這裏,我已經創建了一個樣品協議CustomCell其中確認我已經建立的協議。

1.樣本協議

protocol MyProtocol { 
    func protocolMethod() 

} 

2.自定義子類的細胞

class CustomCell:UITableViewCell,MyProtocol { 

    //Implementation of Protol method 
    func protocolMethod() { 

    } 

} 

3.實現代碼如下使用細胞的

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell:CustomCell = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath) as! CustomCell 

    return cell 
}