Sobes.tech
Junior — Middle

Que informação está contida no objeto DataSource?

sobes.tech IA

Resposta da IA

No iOS, o objeto DataSource geralmente contém as informações necessárias para fornecer dados aos elementos da interface de utilizador, como UITableView ou UICollectionView. É responsável por:

  • O número de secções e elementos em cada secção.
  • A configuração e fornecimento de células para exibição.
  • A gestão dos dados exibidos na lista ou coleção.

Exemplo de um DataSource simples para UITableView:

class MyDataSource: NSObject, UITableViewDataSource {
    let items = ["Maçã", "Banana", "Cereja"]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }
}