2017-07-19 19 views
-1

私が知っているセルを作成したい、特定のプロトコルに準拠しています。しかしSwiftのプロトコルに準拠したオブジェクトを作成します

、私がやろう:

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

これは極端に簡単な方法です。単純な –

答えて

2

セルをサブクラス化して、使用するプロトコルを確認する必要があります。ここではサンプルプロトコルを作成しました。カスタムセル私が作成したプロトコルを確認します。そのセルの

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 
} 
関連する問題