iOS 13 Data Source 新特性:基于 Diffable 实现局部刷新

本文介绍了iOS 13中UITableView的Diffable Data Source新特性,通过对比传统Data Source的使用现状,阐述了Diffable Data Source如何简化局部刷新操作,避免数据不一致异常。并提供了实践案例,展示了如何使用Diffable Data Source进行数据更新和UI同步。
摘要由CSDN通过智能技术生成

点击上方“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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值