• 【Swift】UITableViewDiffableDataSource的用法


      在 iOS 13 中 Apple 为 UITableView 和 UICollectionView 引入了 DiffableDataSource,让开发者可以更简单高效的实现 UITableView、UICollectionView 的局部数据刷新。新的刷新的方法为 apply,通过使用 apply 方法无需计算变更的 indexPaths,也无需调用 reload,即可安全地在主线程或后台线程更新 UI, 仅需简单的将需要变更后的数据通过 NSDiffableDataSourceSnapshot 计算出来。

      1、使用 DiffableDataSource 配置当前 UITableView 的数据源。

    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、在需要刷新的时候,使用 DataSourceSnapshot 处理变更后的数据源,其有 append、delete、move、insert 等方法。DiffableDataSource 通过调用自身 apply 方法将 DataSourceSnapshot 变更后的数据更新同步到 UITableView。

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

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

      实例代码

    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)
            
        }
        
        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
            }
        }
        
        func updateData(_ noteList: [Note]) {
            var snapshot = NSDiffableDataSourceSnapshot<Section, Note>()
            snapshot.appendSections([.today])
            snapshot.appendItems(noteList)
            self.dataSource.apply(snapshot, animatingDifferences: false, completion: nil)
        }
    }
  • 相关阅读:
    [ lucene高级 ] lucene中的算法PriorityQueue
    [ lucene扩展 ] MoreLikeThis 相似检索
    排序08归并排序
    lucene中的数值型字段(NumericField)
    两三年前的搜索管理系统
    java中的集合包简要分析
    倒排索引基础
    散列02java中的hashMap
    Mysql数据库中InnoDB和MyISAM的差别
    ajax 乱码
  • 原文地址:https://www.cnblogs.com/xjf125/p/13130736.html
Copyright © 2020-2023  润新知