Junior — Middle
Can you explain what the TableView component is in user interface development?
sobes.tech AI
Answer from AI
The TableView component in iOS is a user interface element that displays a list of data in the form of a scrollable table. It allows for efficient display of a large number of similar items, divided into sections and rows.
TableView uses a delegation pattern to manage content and behavior: through the protocols UITableViewDataSource and UITableViewDelegate, the developer specifies how many rows there are, how they look, and how to respond to user actions.
An example of simple usage of TableView in Swift:
class ViewController: UIViewController, UITableViewDataSource {
let data = ["Apple", "Banana", "Cherry"]
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", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
Thus, TableView is a powerful and flexible way to display lists in iOS applications.