Junior — Middle
Vysvětlete, jak je zajištěna interakce s buňkami v tabulce dat.
sobes.tech AI
Odpověď od AI
V iOS je interakce s buňkami tabulky obvykle realizována prostřednictvím třídy UITableView a jejích metod. Každá buňka je objektem UITableViewCell, který lze nakonfigurovat a znovu použít.
Hlavní body:
- Pro zobrazení dat se implementuje metoda
tableView(_:cellForRowAt:), kde se vytvoří nebo znovu použije buňka a naplní se potřebnými informacemi. - Pro zpracování klepnutí se používá metoda
tableView(_:didSelectRowAt:), kde lze definovat akce při výběru buňky.
Příklad tvorby a konfigurace buňky:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.textLabel?.text = "Řádek \(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Vybraná buňka \(indexPath.row)")
}