Sobes.tech
Junior — Middle

Aké informácie obsahuje objekt DataSource?

sobes.tech AI

Odpoveď od AI

V iOS je objekt DataSource zvyčajne obsahuje informácie potrebné na poskytovanie údajov prvkom používateľského rozhrania, ako je UITableView alebo UICollectionView. Je zodpovedný za:

  • Počet sekcií a prvkov v každej sekcii.
  • Konfiguráciu a poskytovanie buniek na zobrazenie.
  • Správu údajov, ktoré sa zobrazujú v zozname alebo kolekcii.

Príklad jednoduchého DataSource pre UITableView:

class MyDataSource: NSObject, UITableViewDataSource {
    let items = ["Jablko", "Banán", "Vrch"]

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