Junior — Middle
Can you explain what a UITableView component is and how it is used in interface development?
sobes.tech AI
Answer from AI
UITableView is a UI component in iOS that displays a list of items in a scrollable table. It is widely used for presenting large data sets in a convenient way.
Main features:
- Allows displaying data in the form of rows (cells).
- Supports cell reuse for memory optimization.
- Allows customizing the appearance and behavior of cells.
- Supports sections and headers.
To use UITableView, you need to implement the UITableViewDataSource (for providing data) and UITableViewDelegate (for handling interactions) protocols.
Example of simple usage:
class ViewController: UIViewController, UITableViewDataSource {
let tableView = UITableView()
let data = ["Apple", "Banana", "Orange"]
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
}
}
Thus, UITableView is a powerful and flexible tool for creating lists in iOS applications.