Sobes.tech
Junior — Middle

Vysvetlite, ako sa uskutočňuje interakcia s bunkami v tabuľke údajov.

sobes.tech AI

Odpoveď od AI

V iOS sa interakciou s bunkami tabuľky zvyčajne pracujeme prostredníctvom triedy UITableView a jej metód. Každá bunka je objekt UITableViewCell, ktorý je možné nakonfigurovať a znovu použiť.

Hlavné body:

  • Na zobrazovanie dát implementujeme metódu tableView(_:cellForRowAt:), kde sa vytvorí alebo znovu použije bunka a naplní sa potrebnými informáciami.
  • Na spracovanie kliknutí sa používa metóda tableView(_:didSelectRowAt:), kde môžu byť definované akcie pri výbere bunky.

Príklad tvorby a konfigurácie bunky:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.textLabel?.text = "Riadok \(indexPath.row)"
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Vybraná bunka \(indexPath.row)")
}