Swift中UITableViewDiffableDataSource的使用

在 iOS 13 中 Apple 为 UITableView 和 UICollectionView 引入了 DiffableDataSource,
让开发者可以更简单高效的实现 UITableView、UICollectionView 的局部数据刷新。

新的刷新的方法为 apply
通过使用 apply 方法无需计算变更的 indexPaths,也无需调用 reload,即可安全地在主线程或后台线程更新 UI,
仅需简单的将需要变更后的数据通过 NSDiffableDataSourceSnapshot 计算出来。

主要步骤分2步
1.生成dataSource数据源:var dataSource: UITableViewDiffableDataSource<Section, Note>!
2.根据数据源修改生成变化快照:var snapshot = NSDiffableDataSourceSnapshot<Section, Note>()
3.将dataSource数据源apply应用变更快照,局部刷新UITableView
 
代码举例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import UIKit
  
enum Section {
    case today
}
  
struct Note : Hashable {
    var idx : Int
    var content : String
}
  
class ViewController: UIViewController {
  
    private var dataSource: UITableViewDiffableDataSource<Section, Note>!
     
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self))
         
        self.view.addSubview(tableView)
         
        return tableView
    }()
     
    override func loadView() {
        super.loadView()
         
        self.tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        self.tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    }
  
    override func viewDidLoad() {
        super.viewDidLoad()
        let noteList = [
          Note(idx:0, content:"0"),
          Note(idx:1, content:"1"),
          Note(idx:2, content:"2"),
          Note(idx:3, content:"4")
        ]
       
        self.dataSource = getDataSource()
        updateData(noteList)
         
    }
    //1.使用 DiffableDataSource 配置当前 UITableView 的数据源, 生成UITableViewDiffableDataSource数据源
    func getDataSource() -> UITableViewDiffableDataSource<Section, Note>? {
        return UITableViewDiffableDataSource(tableView: self.tableView) { (tableView, indexPath, note) -> UITableViewCell? in
            let cell = UITableViewCell()
            cell.textLabel?.text = note.content
            return cell
        }
    }
     
    //2.使用snapshot对dataSource进行差异化比对,进行动态更新。避免了之前的复杂的维护过程
    func updateData(_ noteList: [Note]) {
        var snapshot = NSDiffableDataSourceSnapshot<Section, Note>()
        snapshot.appendSections([.today])
        snapshot.appendItems(noteList)
        //3.使用修改后的新数据snapshot,传入apply方法内,让新旧数据进行对比,然后在内部对变更后的差异数据源进行更新。
        //DiffableDataSource 通过调用自身 apply 方法将 DataSourceSnapshot 变更后的数据更新同步到 UITableView。
        self.dataSource.apply(snapshot, animatingDifferences: false, completion: nil)
    }
}

 

 

参考文章:
https://blog.csdn.net/Landen2011/article/details/125603728

 

 

posted @   滴水微澜  阅读(570)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示