ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4126Uitableview Datasource…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4126

    Mobile Application Development 2

    Progress0 / 38 done
    Topics38
    1Understanding Xcode2Using the Xcode interface builder and objects library3Understanding view hierarchy and creating a custom app icon
    4Understanding outlets and actions5Using text fields, buttons, labels, web views, and page controllers6Using views with subviews and creating views using code
    7Working with the single view template8Exploring the app delegate and adding new view controllers9Transitioning between multiple view controllers using animations
    10Tabbar and master detail templates
    11Customizing for different inputs12Adjusting text field behaviors and dismissing the keyboard13Detecting keyboard activities with notification center14Using scroll view and responding to keyboard activities programmatically
    15Detecting device hardware16Dynamically adjusting graphical layouts and creating universal apps
    17Understanding UITableView and UITableViewCell18Working with UITableView data source and delegate19Master detail template, drill-down menus, and navigation20Using property lists for data persistence and creating multi-section tables
    21Portrait and landscape modes22Handling device rotation and forcing specific orientation23Dynamically adjusting layouts based on rotation
    24Importing sqlite3 and creating a database25Writing tables, inserting records, and bundling a database with your app26Checking for database existence and reading/displaying data
    27NSTimer class and object transformations28Rotation, scaling, translation, animating image arrays, and playing video
    29Email, Safari, and SMS30Working with camera and photo library
    31Consuming and parsing XML and JSON32Integrating Twitter and Facebook with iOS apps
    33MapKit and MKMapView34Getting and displaying user location and directional information35Displaying map annotations, disclosure buttons, and reverse geocoding36Working with iCloud
    37Gyroscope and accelerometer38Outputting sensor data and using the Shake API
    4 min read
    Topic 18 of 38
    Topic Study Note Illustration
    18

    Working with UITableView data source and delegate

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

    πŸ“± 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    638words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2