How to Use DiffableDataSource with UI TableView in Swift IOS

In this article, we will learn how to use DiffableDataSource with UI TableView in Swift IOS. A modified data source object is a special type of data source that works with the table view object. It provides the behavior needed to easily and efficiently manage spreadsheet data and UI updates.

Diffable Data Source with UI TableView - Handy Opinion

UIViewController Code

UITableViewDiffableDataSource an object that you use to manage data and make cells available for a table view.

NSDiffableDataSourceSnapshot Snapshots use to provide data for data source view and table views. A snapshot determines the initial state of the data displayed in a view, and then the data is updated.

The data in the snapshot contains the sections and items you want to view.

import UIKit

class DiffableViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var datasource : UITableViewDiffableDataSource<Section,User>!
    var usersSectionA = [User]()
    var usersSectionB = [User]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadTableView()
    }
    
    func loadTableView() {
        
        self.datasource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
            cell.label.text = item.name
            return cell
        })
                
        usersSectionA.append(User(id: "1", name: "Ali"))
        usersSectionA.append(User(id: "2", name: "Abdul"))
        usersSectionA.append(User(id: "3", name: "Alice"))
                
        usersSectionB.append(User(id: "4", name: "Brooke"))
        usersSectionB.append(User(id: "5", name: "Bobby"))
        usersSectionB.append(User(id: "6", name: "Ben"))
                
        var snapshot = NSDiffableDataSourceSnapshot<Section, User>()
        snapshot.appendSections([Section.A, Section.B])
        snapshot.appendItems(usersSectionA, toSection: Section.A)
        snapshot.appendItems(usersSectionB, toSection: Section.B)
        datasource.apply(snapshot)
        
    }
}

Data Model Class

In the model class we have the the user ID and and Name and having operator overload function.

import Foundation

enum Section {
    case A
    case B
}

struct User : Hashable {
    var id = ""
    var name = ""
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: User, rhs: User) -> Bool {
        return lhs.id == rhs.id
    }
}

In this tutorial, we have learned how to use DiffableDataSource with UI TableView in Swift IOS.

If you have any questions or suggestions, feel free to ask in the comments section below

Next Article

1. How to Show Local Notification in Swift IOS
2. UITextField for Emoji Input & Unchangeable Keyboard Type in Swift IOS
3. Get Device Identifier (UUID) and save in Keychain in Swift IOS

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: