2017-07-19 137 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 
}