Sobes.tech
Junior — Middle

Jakie informacje zawiera obiekt DataSource?

sobes.tech AI

Odpowiedź od AI

W iOS obiekt DataSource zazwyczaj zawiera informacje niezbędne do dostarczania danych elementom interfejsu użytkownika, takim jak UITableView lub UICollectionView. Jest odpowiedzialny za:

  • Liczbę sekcji i elementów w każdej sekcji.
  • Konfigurację i dostarczanie komórek do wyświetlenia.
  • Zarządzanie danymi wyświetlanymi na liście lub w kolekcji.

Przykład prostego DataSource dla UITableView:

class MyDataSource: NSObject, UITableViewDataSource {
    let items = ["Jabłko", "Banan", "Wiśnia"]

    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
    }
}