0

I want to create cell that i know, is conformable to specific protocol.

However, when i try to do :

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

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

        return cell
    }

I got an error. How to fix it?

7
  • you can make an extension to all tableview cell or that specific cell class and conform to that protocol Commented Jul 19, 2017 at 12:03
  • @Lu_ i really believe that there is an easier way Commented Jul 19, 2017 at 12:04
  • this is extremally easy way, how do you want to make anything easier than that, it conforms or not, that simple Commented Jul 19, 2017 at 12:05
  • let cell : ModelBinding = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath) as! ModelBinding Commented Jul 19, 2017 at 12:08
  • 1
    @EvgeniyKleban So you have your solution ;) Other answers are correct one for question you've asked. Commented Jul 19, 2017 at 12:19

1 Answer 1

2

You have to subclass cell , which confirms the protocol that you want to use. Here i have created a sample protocol and CustomCell which confirming the protocol i have created.

1. Sample protocol

protocol MyProtocol {
    func protocolMethod()

}

2. Custom subClassed cell

class CustomCell:UITableViewCell,MyProtocol {

    //Implementation of Protol method
    func protocolMethod() {

    }

}

3. Use of that cell on tableview

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

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

    return cell
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.