Sobes.tech
Junior — Middle

Jaké informace obsahuje objekt DataSource?

sobes.tech AI

Odpověď od AI

V iOS je objekt DataSource obvykle obsahuje informace potřebné k poskytování dat prvkům uživatelského rozhraní, jako je UITableView nebo UICollectionView. Je zodpovědný za:

  • Počet sekcí a prvků v každé sekci.
  • Konfiguraci a poskytování buněk pro zobrazení.
  • Správu dat, která jsou zobrazena v seznamu nebo kolekci.

Příklad jednoduchého DataSource pro UITableView:

class MyDataSource: NSObject, UITableViewDataSource {
    let items = ["Jablko", "Banán", "Třešeň"]

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