Junior — Middle
Kokia informacija yra DataSource objekte?
sobes.tech AI
Atsakymas iš AI
iOS objektas DataSource paprastai turi informaciją, reikalingą duomenims teikti naudotojo sąsajos elementams, tokiems kaip UITableView arba UICollectionView. Jis atsakingas už:
- Sekcijų ir elementų skaičių kiekvienoje sekcijoje.
- Konfigūraciją ir teikimą langelių, skirtų rodymui.
- Duomenų valdymą, kurie rodomi sąraše arba kolekcijoje.
Pavyzdys paprasto DataSource, skirto UITableView:
class MyDataSource: NSObject, UITableViewDataSource {
let items = ["Obuolys", "Bananas", "Vyš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
}
}