
A UITableView is a UI component in iOS used to display data in a scrollable list format (rows and sections) .
UITableView and UITableViewCell (iOS β Xcode)A UITableView is a UI component in iOS used to display data in a scrollable list format (rows and sections).
π Example:
A UITableViewCell is a single row inside a table view that displays one item of data.
π Example:
Table View
β
Section (optional)
β
Rows (Cells)
Provides data to the table view
Required methods define:
Draw:
TableView
βββ Cell 1 (Row)
βββ Cell 2 (Row)
βββ Cell 3 (Row)
@IBOutlet weak var tableView: UITableView!
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Row \(indexPath.row)"
return cell
}
UITableViewCell@IBOutlet weak var nameLabel: UILabel!
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyTableViewCell
cell.nameLabel.text = "Student \(indexPath.row)"
return cell
Old cells β reused β new data assigned
cellForRowAtlet contacts = ["Ali", "Sara", "Ahmed", "Ayesha"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = contacts[indexPath.row]
return cell
}
UITableView β displays list of data
UITableViewCell β single row in list
Uses:
Key methods:
numberOfRowsInSectioncellForRowAtCell reuse improves performance
Custom cells used for advanced UI
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.