在 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. 【Boost】Windows 下个人在配置 Boost 踩到的坑以及解决方案

    要编译的 Boost 版本:1.82.0 假设 Boost 的根目录为 ${boost_root}$ 如果不想编译可以使用博主编译后的Boost:https://pan.baidu.com/s/1s2 ...

  2. Golang之文件系统事件监听

    Golang之文件系统事件监听 基本介绍 文件系统事件是指文件系统相关的各种操作和状态变化,当一个应用层的进程操作文件或目录时,会触发system call,内核的notification子系统可以守 ...

  3. 6、SpringBoot-mybatis分页实现pagehelper

    系列导航 springBoot项目打jar包 1.springboot工程新建(单模块) 2.springboot创建多模块工程 3.springboot连接数据库 4.SpringBoot连接数据库 ...

  4. 一、mysql5.7 rpm 安装(单机)

    一.下载需要的rpm包mysql-community-client-5.7.26-1.el6.x86_64.rpmmysql-community-common-5.7.26-1.el6.x86_64. ...

  5. uniapp#实现自定义省市区三级联动

    uni-APP中的三级联动(省市区)---数据前端写死 https://blog.csdn.net/lwaner/article/details/107150805 uniapp#实现自定义省市区三级 ...

  6. nextTick使用

  7. 每天学五分钟 Liunx | 有趣的 log

    说明:看 systemd log 的时候发现了一段有意思的打印,不太明白为什么会这样,贴出来与朋友们分享,欢迎知道的朋友们说明下,非常感谢.   问题描述:服务启动时,会执行 python 脚本,该脚 ...

  8. The project description file (.project) for XXX is missing

    在STS中切换项目分支的时候,出现一个项目打不开了,提示:The project description file (.project) for XXX is missing 试了下网上的方法都没有解 ...

  9. Java开发者的Python进修指南:JSON利器之官方json库、demjson和orjson的实用指南

    JSON JSON作为目前最流行的传输格式,在Python中也有相应的实现方式.由于JSON格式的文本可以跨平台并且简单易用,因此被广泛传播.因此,我们今天的主要讨论内容是如何熟练地应用Python的 ...

  10. [转帖]signal 11 (SIGSEGV)错误排查

    https://www.jianshu.com/p/a4250c72d391 jni调试最蛋疼的就是signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault a ...