ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development 2
    COMP4126
    Progress0 / 38 topics
    Topics
    1. Creating an iOS App: Understanding Xcode2. Using the Xcode interface builder and objects library3. Understanding view hierarchy and creating a custom app icon4. Outlets, Actions, and Views: Understanding outlets and actions5. Using text fields, buttons, labels, web views, and page controllers6. Using views with subviews and creating views using code7. Using View Controllers: Working with the single view template8. Exploring the app delegate and adding new view controllers9. Transitioning between multiple view controllers using animations10. Application Templates: Tabbar and master detail templates11. The iOS Keyboard: Customizing for different inputs12. Adjusting text field behaviors and dismissing the keyboard13. Detecting keyboard activities with notification center14. Using scroll view and responding to keyboard activities programmatically15. Working with Different iOS Devices (iPhone & iPad): Detecting device hardware16. Dynamically adjusting graphical layouts and creating universal apps17. Using Table Views: Understanding UITableView and UITableViewCell18. Working with UITableView data source and delegate19. Master detail template, drill-down menus, and navigation20. Using property lists for data persistence and creating multi-section tables21. Supporting Screen Rotations: Portrait and landscape modes22. Handling device rotation and forcing specific orientation23. Dynamically adjusting layouts based on rotation24. Working with Databases: Importing sqlite3 and creating a database25. Writing tables, inserting records, and bundling a database with your app26. Checking for database existence and reading/displaying data27. Using Animations & Video: NSTimer class and object transformations28. Rotation, scaling, translation, animating image arrays, and playing video29. Accessing Integrated iOS Apps: Email, Safari, and SMS30. Working with camera and photo library31. Using Web Services: Consuming and parsing XML and JSON32. Integrating Twitter and Facebook with iOS apps33. Working with iOS Maps and Location Services: MapKit and MKMapView34. Getting and displaying user location and directional information35. Displaying map annotations, disclosure buttons, and reverse geocoding36. Working with iCloud37. Working with the Accelerometer: Gyroscope and accelerometer38. Outputting sensor data and using the Shake API
    COMP4126›Working with UITableView data source and delegate
    Mobile Application Development 2Topic 18 of 38

    Working with UITableView data source and delegate

    4 minread
    638words
    Beginnerlevel

    📱 Working with UITableView Data Source and Delegate (iOS – Xcode)


    ✅ 1. Definition

    🔹 UITableView Data Source

    The Data Source is responsible for providing data to the table view. It tells the table view:

    • How many rows to display
    • What content each row (cell) should show

    🔹 UITableView Delegate

    The Delegate handles user interactions and appearance behavior of the table view. It controls:

    • Row selection
    • Row height
    • Editing actions

    👉 Simple idea:

    • Data Source = gives data
    • Delegate = controls behavior

    🧠 2. Key Concepts

    🔹 UITableView Protocols

    To use a table view, your ViewController must conform to:

    UITableViewDataSource
    UITableViewDelegate
    

    🔹 IndexPath

    • Represents the position of a cell

    • Contains:

      • section
      • row

    🏗️ 3. Structure of Table View System

    UITableView
       ↓
    DataSource → provides data
    Delegate   → handles interaction
       ↓
    UITableViewCell (rows)
    

    ⚙️ 4. Step-by-Step Implementation


    🔹 Step 1: Connect Table View

    @IBOutlet weak var tableView: UITableView!
    

    🔹 Step 2: Set Delegate and Data Source

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        tableView.delegate = self
    }
    

    🔹 Step 3: Create Data Array

    let fruits = ["Apple", "Banana", "Mango", "Orange"]
    

    📦 5. Data Source Methods


    🔹 1. Number of Rows

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }
    

    👉 Tells how many rows to display


    🔹 2. Cell for Row

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = fruits[indexPath.row]
        return cell
    }
    

    👉 Creates and configures each cell


    🎯 6. Delegate Methods


    🔹 1. Row Selection

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

    👉 Called when user taps a row


    🔹 2. Row Height

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    

    👉 Controls row size


    🔹 3. Deselect Row (Good Practice)

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    

    📊 7. Diagram Description (for Exams)

    Draw:

    UITableView
       ↓
    DataSource → rows + content
    Delegate   → tap + behavior
       ↓
    Cells (UITableViewCell)
    

    💡 8. Example App

    🎯 Fruits List App

    let fruits = ["Apple", "Banana", "Mango"]
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = fruits[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected \(fruits[indexPath.row])")
    }
    

    📌 9. Important Rules / Tips

    • Always set dataSource and delegate
    • Data source handles data only
    • Delegate handles user actions
    • Use IndexPath.row to access array data
    • Always reload table after data changes:
    tableView.reloadData()
    

    ⚠️ 10. Common Mistakes

    • ❌ Forgetting to set delegate/dataSource
    • ❌ Wrong index usage
    • ❌ Not reloading table after updating data
    • ❌ Returning wrong number of rows
    • ❌ Crashing due to out-of-range index

    🧠 11. Best Practices

    • Use arrays or models for data
    • Separate UI logic from data logic
    • Use custom cells for complex UI
    • Always handle selection properly
    • Keep delegate methods lightweight

    📝 12. Likely Exam Questions

    1. What is UITableViewDataSource?
    2. What is UITableViewDelegate?
    3. Differentiate between data source and delegate.
    4. Explain cellForRowAt method.
    5. What is IndexPath used for?
    6. Write code for table view setup.
    7. Explain didSelectRowAt method.
    8. Why is reloadData() used?

    📚 13. Quick Revision Summary

    • Data Source → provides data (rows + content)

    • Delegate → handles user interaction

    • Key methods:

      • numberOfRowsInSection
      • cellForRowAt
      • didSelectRowAt
    • Uses IndexPath to access data

    • reloadData() updates table view

    • Essential for all list-based apps


    Previous topic 17
    Using Table Views: Understanding UITableView and UITableViewCell
    Next topic 19
    Master detail template, drill-down menus, and navigation

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count638
      Code examples0
      DifficultyBeginner