📱 Using Table Views: Understanding UITableView and UITableViewCell (iOS – Xcode)
✅ 1. Definition
🔹 UITableView
A UITableView is a UI component in iOS used to display data in a scrollable list format (rows and sections).
👉 Example:
- Contacts list
- Messages
- Settings menu
🔹 UITableViewCell
A UITableViewCell is a single row inside a table view that displays one item of data.
👉 Example:
- One contact name = one cell
- One message = one cell
🧠 2. Key Concepts
🔹 Table View Structure
Table View
↓
Section (optional)
↓
Rows (Cells)
🔹 Data Source
🔹 Delegate
- Handles user interaction
- Example: selecting a row
🏗️ 3. UITableView Components
🔹 1. UITableView
Features:
- Scrollable list
- Supports sections
- Reusable cells for performance
🔹 2. UITableViewCell
Features:
- Displays text, images, or custom UI
- Reused to improve performance
📊 Diagram Description (for Exams)
Draw:
TableView
├── Cell 1 (Row)
├── Cell 2 (Row)
├── Cell 3 (Row)
⚙️ 4. Steps to Use Table View in Xcode
🔹 Step 1: Add Table View
- Drag UITableView into storyboard
🔹 Step 2: Create Outlet
@IBOutlet weak var tableView: UITableView!
🔹 Step 3: Set Data Source & Delegate
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
🔹 Step 4: Provide Number of Rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
🔹 Step 5: Configure Cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Row \(indexPath.row)"
return cell
}
🧩 5. Custom UITableViewCell
🔹 Step 1: Create Custom Cell
- File → New → Cocoa Touch Class
- Subclass:
UITableViewCell
🔹 Step 2: Design in Storyboard
🔹 Step 3: Connect Outlets
@IBOutlet weak var nameLabel: UILabel!
🔹 Step 4: Use Custom Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyTableViewCell
cell.nameLabel.text = "Student \(indexPath.row)"
return cell
🔄 6. Cell Reusability (Important Concept)
🔹 Why reuse cells?
- Improves performance
- Reduces memory usage
Old cells → reused → new data assigned
📌 7. Important Rules / Tips
- Always set dataSource and delegate
- Use reuseIdentifier for cells
- Avoid heavy processing inside
cellForRowAt
- Use custom cells for complex UI
- Keep data separate from UI logic
💡 8. Example App
🎯 Contacts App
- Table View shows list of names
let 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
}
⚠️ 9. Common Mistakes
- ❌ Not setting delegate/dataSource
- ❌ Wrong reuse identifier
- ❌ Forgetting to return number of rows
- ❌ Crashes due to force unwrapping custom cells
- ❌ Not registering custom cell
🧠 10. Best Practices
- Use custom cells for complex layouts
- Always reuse cells properly
- Keep data in arrays or models
- Separate UI and logic
- Use MVC pattern
📝 11. Likely Exam Questions
- What is UITableView?
- Define UITableViewCell.
- Explain the structure of a table view.
- What is cell reuse? Why is it important?
- Write code to display data in table view.
- What are data source and delegate methods?
- How do you create a custom table view cell?
- Explain reuseIdentifier in UITableViewCell.
📚 12. Quick Revision Summary
-
UITableView → displays list of data
-
UITableViewCell → single row in list
-
Uses:
- DataSource → provides data
- Delegate → handles interaction
-
Key methods:
numberOfRowsInSection
cellForRowAt
-
Cell reuse improves performance
-
Custom cells used for advanced UI