Sobes.tech
Junior — Middle

Môžete vysvetliť, čo je komponenta UITableView a ako sa používa pri vývoji rozhraní?

sobes.tech AI

Odpoveď od AI

UITableView — je komponenta rozhrania v iOS, ktorá zobrazuje zoznam prvkov vo forme tabuľky so skrolovaním. Je široko používaná na prezentáciu veľkých súborov dát v pohodlnom formáte.

Hlavné vlastnosti:

  • Umožňuje zobrazovať dáta vo forme riadkov (bunkách).
  • Podporuje opätovné použitie buniek na optimalizáciu pamäti.
  • Umožňuje prispôsobiť vzhľad a správanie buniek.
  • Podporuje sekcie a záhlavia.

Na použitie UITableView je potrebné implementovať protokoly UITableViewDataSource (pre poskytovanie dát) a UITableViewDelegate (pre spracovanie interakcií).

Príklad jednoduchého použitia:

class ViewController: UIViewController, UITableViewDataSource {
    let tableView = UITableView()
    let data = ["Jablko", "Banán", "Pomaranč"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.frame = view.bounds
        tableView.dataSource = self
        view.addSubview(tableView)
    }

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

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

Takto je UITableView mocný a flexibilný nástroj na tvorbu zoznamov v iOS aplikáciách.