点击上方“iOSTps”,选择“星标”
在看 真爱
作者: 老峰
本文是老峰 6 月贡献在《WWDC19 内参》的文章,上周 iOS 13 正式发布,专栏也免费开放,所以同步分享到个人公众号,还没订阅的读者朋友可以点击 阅读原文 免费订阅,干货多多!
引言
在 iOS 开发中,UITableView 和 UICollectionView 是很常用的 UI 控件,在过去我们通常需要实现 Data Sources 来配置数据源,虽然在简单的业务中我们可以愉快的实现各种需求,可是一旦业务复杂起来,比如数据源实时的增删改,我们经常会一不小心就遇到 NSInternalInconsistencyException(Data Source 和当前 UI 状态不一致)等奇奇怪怪的异常,本文基于 WWDC 2019 Session 220:Advances in UI Data Sources 将从以下三部分分享此 Session 及老峰的实践心得:
Data Source 使用现状
iOS 13 Diffable Data Source 新 API
Diffable Data Source 实践
1、Data Source 使用现状
这里以 Session 中 WiFi 设置为例,我们实现一个无线局域网列表页面如下图所示:
按照通常实现方式我们首先需要实现 UITableView 的 Data Source 方法
func numberOfSections(in tableView: UITableView) -> Int {
return models.count
}
// Return the number of rows for the table.
func tableView(_ tableView: UITableView, numberOfRowsInSection p: Int) -> Int {
return models[p].count
}
// Provide a cell object for each row.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Fetch a cell of the appropriate type.
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
// Configure the cell’s contents.
cell.textLabel!.text = "WiFi text"
return cell
}
由于每个网络可用网络的状态都在实时变化如网络 abert 路由器重启,新的网络 interna