在 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
 
代码举例如下:
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

Swift中UITableViewDiffableDataSource的使用的更多相关文章

  1. swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明

    关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...

  2. 阿里巴巴最新开源项目 - [HandyJSON] 在Swift中优雅地处理JSON

    项目名称:HandyJSON 项目地址:https://github.com/alibaba/handyjson 背景 JSON是移动端开发常用的应用层数据交换协议.最常见的场景便是,客户端向服务端发 ...

  3. Swift中的可选链与内存管理(干货系列)

    干货之前:补充一下可选链(optional chain) class A { var p: B? } class B { var p: C? } class C { func cm() -> S ...

  4. 在Swift中实现单例方法

    在写Swift的单例方法之前可以温习一下Objective-C中单例的写法: + (instancetype)sharedSingleton{ static id instance; static d ...

  5. [翻译]理解Swift中的Optional

    原文出处:Understanding Optionals in Swift 苹果新的Swift编程语言带来了一些新的技巧,能使软件开发比以往更方便.更安全.然而,一个很有力的特性Optional,在你 ...

  6. 窥探Swift之使用Web浏览器编译Swift代码以及Swift中的泛型

    有的小伙伴会问:博主,没有Mac怎么学Swift语言呢,我想学Swift,但前提得买个Mac.非也,非也.如果你想了解或者初步学习Swift语言的话,你可以登录这个网站:http://swiftstu ...

  7. swift 中指针的使用UnsafeMutablePointer

    在swift中已经弱化了指针的使用,可以这么使用 let s: NSRange = NSMakeRange(, ) let at = UnsafeMutablePointer<NSRange&g ...

  8. swift 中数据类型那个的转换

    在swift中关于数据类型的转换,如果参数是可选类型? 那么打印或者转换的结果 会带有Optional 字样,,

  9. swift中Range的使用书名

    在swift中Range有两种用法 1.把字符串转换成NSString来使用 //这里是把swift的字符换转换成了nsstring 使用 let str :NSString = text.strin ...

  10. 【iOS】在Swift中使用JSONModel

    前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...

随机推荐

  1. proxy代理

  2. 【TouchGFX】Widgets and Containers

    Widgets and Containers 是 TouchGFX 应用最基础的东西,他们贯穿于 UI 的整个开发,属于 TouchGFX 的预制组件,同时也支持自定义实现 Widgets Widge ...

  3. Data truncated for column '字段名' at row 1 的解决方法

    1.原因: 修改表结构 XXX 为 not null 时,表数据  XXX 字 段 存在 null 值. 2.解决: 去掉或修改 带有 null 值 的 ( 需要设置 not null 的) 字段

  4. CSS - 使用CSS 3D属性来完成页面视差滚动效果。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Linux复习笔记

    Linux复习笔记 常识说明 目录结构 Linux以树型结构管理文件,其最上层文件夹为 / ,也就是根目录. 如图所示,图中展示了一部分文件夹的结构: 所有的文件夹都属于根目录的子文件夹. 安装好系统 ...

  6. window-子系统-ubuntu

    window-子系统-ubuntu 1. 背景 提供类Linux开发环境(命令行.文件系统.进程管理.网路) 2. 安装 A. wsl 安装 下载链接: https://wslstorestorage ...

  7. [转帖]你应该知道的Shell 脚本的经典十三问

    https://blog.csdn.net/wangzhicheng987/article/details/131031344 1. 为何叫做shell? 我们知道计算机的运作不能离开硬件,但使用者却 ...

  8. [转帖]SIMD指令集 SSE/AVX

    SIMD指令集 SSE/AVX 概述 参考手册 Intel Intrinsics Guide Tommesani.com Docs Intel 64 and IA-32 Architectures S ...

  9. [转帖]LVS入门篇(五)之LVS+Keepalived实战

    LVS入门篇(五)之LVS+Keepalived实战 https://www.cnblogs.com/linuxk/p/9365189.html 一.实验架构和环境说明 (1)本次基于VMware W ...

  10. 申威3231服务器Redis性能验证-及最全信创CPU性能分析

    申威3231服务器Redis性能验证-及最全信创CPU性能分析 背景 公司里面新进了几台服务器. 有台申威服务器. 因为前段时间参与过一次申威的POC验证. 当时对性能有一点简单的理解. 但是因为不方 ...