美文网首页
UITableViewDiffableDataSource的使用

UITableViewDiffableDataSource的使用

作者: X_L_F | 来源:发表于2019-11-18 09:45 被阅读0次

背景

UITableViewDiffableDataSource 出现的原因,主要是列表数据在刷新时。若使用reloadData方法,会进行所有数据的刷新(明显比较耗资源);若要进行局部刷新,reloadRows或者reloadSection等,这部分的数据刷新需要程序员自己进行比对(过程比较复杂,且容易出错)

使用方式

1.准备数据源

数据类型必选是Hashable类型

// Model.swift
enum Section {
  case today
}

struct Note: Hashable {
  var id: Int
  var content: String
}

2.使用UITableViewDiffableDataSource

// UIViewController.swift
// UITableViewDiffableDataSource泛型第一个参数是section类型,第二个参数是item类型
private var dataSource: UITableViewDiffableDataSource<Section, Note>!

override func viewDidLoad() {
  self.dataSource = getDataSource()
}

func getDataSource() -> UITableViewDiffableDataSource<Section, Note>? {
  return UITableViewDiffableDataSource(tableView: self.tableView) {
    (tableView, index, note) -> UITableViewCell? in 
      let cell = UITableViewCell()
        cell.textLabel?.text = note.content
    
        return cell
  }
}

UITableViewDiffableDataSource的初始化方法

public init(tableView: UITableView, cellProvider: @escaping UITableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider)

其中CellProvider定义如下

public typealias CellProvider = (UITableView, IndexPath, ItemIdentifierType) -> UITableViewCell?

3.更新数据

// UIViewController.swift
override func viewDidLoad() {
  let noteList = [
    Note(id:0, content:"0"),
    Note(id:1, content:"1"),
    Note(id:2, content:"2"),
    Note(id:3, content:"4")
  ]
  updateData(noteList)
}

func updateData(_ noteList: [Note]) {
  var snapshot = NSDiffableDataSourceSnapshot<Section, Note>()
  snapshot.appendSections([.today])     // 对应的section
  snapshot.appendItems(noteList)            // 对应的item
  self.dataSource.apply(snapshot, animatingDifferences: false, completion: nil)
}

主要靠出入snapshot让dataSource进行数据更新。需要注意的是,初始化数据和更新数据都是用到用到snapshot

原理

使用snapshot对dataSource进行差异化比对,进行动态更新。避免了之前的复杂的维护过程

完整代码

// Model.swift
import Foundation

enum Section {
    case today
}

struct Note: Hashable {
    var id: Int
    var content: String
}
import UIKit

class ViewController: UIViewController {
    private var dataSource: UITableViewDiffableDataSource<NoteSection, 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()
        // Do any additional setup after loading the view.
        let noteList = [
          Note(id:0, content:"0"),
          Note(id:1, content:"1"),
          Note(id:2, content:"2"),
          Note(id:3, content:"4")
        ]
      
        self.dataSource = getDataSource()
        updateData(noteList)
        
    }
    
    func getDataSource() -> UITableViewDiffableDataSource<NoteSection, Note>? {
        return UITableViewDiffableDataSource(tableView: self.tableView) { (tableView, indexPath, note) -> UITableViewCell? in
            let cell = UITableViewCell()
            cell.textLabel?.text = note.content
            return cell
        }
    }
    
    func updateData(_ noteList: [Note]) {
        var snapshot = NSDiffableDataSourceSnapshot<NoteSection, Note>()
        snapshot.appendSections([.today])
        snapshot.appendItems(noteList)
        self.dataSource.apply(snapshot, animatingDifferences: false, completion: nil)
    }
}

相关文章

  • UITableViewDiffableDataSource的使用

    背景 UITableViewDiffableDataSource 出现的原因,主要是列表数据在刷新时。若使用rel...

  • UITableViewDiffableDataSource和UI

    在 iOS 平台上是 UITableViewDiffableDataSource 和 UICollectionVi...

  • UITableViewDiffableDataSource--i

    今天面试遇到了这个问题,最近一直关注业务,没有关注到新api,所以被问到UITableViewDiffableDa...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

网友评论

      本文标题:UITableViewDiffableDataSource的使用

      本文链接:https://www.haomeiwen.com/subject/wlsfictx.html