Sobes.tech
Junior — Middle

Explain how interaction with data table cells is carried out.

sobes.tech AI

Answer from AI

In iOS, interaction with table view cells usually occurs through the UITableView class and its methods. Each cell is a UITableViewCell object that can be customized and reused.

Main points:

  • To display data, the method tableView(_:cellForRowAt:) is implemented, where a cell is created or reused and filled with the necessary information.
  • To handle taps, the method tableView(_:didSelectRowAt:) is used, where actions upon cell selection can be defined.

Example of creating and configuring a cell:

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Selected cell \(indexPath.row)")
}