Una clase tiene que heredar de una clase padre antes de cumplir con el protocolo. Hay principalmente dos formas de hacerlo.
Una forma es hacer que su clase herede NSObjecty se ajuste a la clase UITableViewDataSource. Ahora, si desea modificar las funciones en el protocolo, debe agregar una palabra clave overrideantes de la llamada a la función, de esta manera
class CustomDataSource : NSObject, UITableViewDataSource {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
}
Sin embargo, esto a veces ensucia su código porque puede tener muchos protocolos que cumplir y cada protocolo puede tener varias funciones de delegado. En esta situación, puede separar el código conforme del protocolo de la clase principal mediante el uso extension, y no necesita agregar una overridepalabra clave en la extensión. Entonces el equivalente del código anterior será
class CustomDataSource : NSObject{
// Configure the object...
}
extension CustomDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
}