View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Advances in diffable data sources

    Diffable data sources dramatically simplify the work involved in managing and updating collection and table views to create dynamic and responsive experiences in your apps. Discover how you can use section snapshots to efficiently build lists and outline collection views for iOS and iPadOS and provide support for implementing the sidebar in an iPad app. We'll also show you how to simplify cell reordering using UICollectionViewDiffableDataSource to help you streamline your code and build app interfaces more quickly.

    This session builds on 2019's “Advances in UI Data Sources,” which you may want to check out first.

    Recursos

    • Implementing modern collection views
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC21

    • Make blazing fast lists and collection views

    WWDC20

    • Lists in UICollectionView
    • Modern cell configuration

    WWDC19

    • Advances in UI Data Sources
  • Buscar neste vídeo...
    • 3:24 - NSDiffableDataSourceSectionSnapshot

      // NSDiffableDataSourceSectionSnapshot
      
      public struct NSDiffableDataSourceSectionSnapshot<ItemIdentifierType> where ItemIdentifierType : Hashable {
      
          public init()
      
          public init(_ snapshot: NSDiffableDataSourceSectionSnapshot<ItemIdentifierType>)
      
          public mutating func append(_ items: [ItemIdentifierType], to parent: ItemIdentifierType? = nil)
      
          public mutating func insert(_ items: [ItemIdentifierType], before item: ItemIdentifierType)
      
          public mutating func insert(_ items: [ItemIdentifierType], after item: ItemIdentifierType)
      
          public mutating func delete(_ items: [ItemIdentifierType])
      
          public mutating func deleteAll()
      
          public mutating func expand(_ items: [ItemIdentifierType])
      
          public mutating func collapse(_ items: [ItemIdentifierType])
      
          public mutating func replace(childrenOf parent: ItemIdentifierType, using snapshot: NSDiffableDataSourceSectionSnapshot<ItemIdentifierType>)
      
          public mutating func insert(_ snapshot: NSDiffableDataSourceSectionSnapshot<ItemIdentifierType>, before item: (ItemIdentifierType))
      
          public mutating func insert(_ snapshot: NSDiffableDataSourceSectionSnapshot<ItemIdentifierType>, after item: (ItemIdentifierType))
      
          public func isExpanded(_ item: ItemIdentifierType) -> Bool
      
          public func isVisible(_ item: ItemIdentifierType) -> Bool
      
          public func contains(_ item: ItemIdentifierType) -> Bool
      
          public func level(of item: ItemIdentifierType) -> Int
      
          public func index(of item: ItemIdentifierType) -> Int?
      
          public func parent(of child: ItemIdentifierType) -> ItemIdentifierType?
      
          public func snapshot(of parent: ItemIdentifierType, includingParent: Bool = false) -> NSDiffableDataSourceSectionSnapshot<ItemIdentifierType>
      
          public var items: [ItemIdentifierType] { get }
      
          public var rootItems: [ItemIdentifierType] { get }
      
          public var visibleItems: [ItemIdentifierType] { get }
      }
    • 4:20 - UICollectionViewDiffableDataSource Additions for Section Snapshots

      // UICollectionViewDiffableDataSource additions for iOS 14
      
      extension UICollectionViewDiffableDataSource<Item, Section> {
      
          func apply(_ snapshot: NSDiffableDataSourceSectionSnapshot<Item>, 
                     to section: Section, 
                     animatingDifferences: Bool = true, 
                     completion: (() -> Void)? = nil)
      
          func snapshot(for section: Section) ->   
                        NSDiffableDataSourceSectionSnapshot<Item>
      }
    • 4:43 - Using Snapshots and Section Snapshots together

      // Example of using snapshots and section snapshots together
      
      func update(animated: Bool=true) {
      
         // Add our sections in a specific order
         let sections: [Section] = [.recent, .top, .suggested]
         var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
         snapshot.appendSections(sections)
         dataSource.apply(snapshot, animatingDifferences: animated)
      
         // update each section's data via section snapshots in the existing position
         for section in sections {
            let sectionItems = items(for: section)
            var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
            sectionSnapshot.append(sectionItems)
            dataSource.apply(sectionSnapshot, to: section, animatingDifferences:animated)
         }
      }
    • 5:18 - Creating hierarchical data with NSDiffableDataSourceSectionSnapshot

      // Create hierarchical data for our Outline
      
      var sectionSnapshot = ...
      
      sectionSnapshot.append(["Smileys", "Nature", 
                              "Food", "Activities",
                              "Travel", "Objects", "Symbols"])
      
      sectionSnapshot.append(["🥃", "🍎", "🍑"], to: "Food")
    • 6:01 - Child Section Snapshots

      let childSnapshot = sectionSnapshot.snapshot(for: parent, includingParent: false)
    • 6:11 - Section Snapshot Expand / Collapse API

      struct NSDiffableDataSourceSectionSnapshot<Item: Hashable> {
         func expand(_ items: [Item])
         func collapse(_ items: [Item])
         func isExpanded(_ item: Item) -> Bool
      }
    • 7:21 - Section Snapshot Handlers

      // Section Snapshot Handlers: handling user interactions for expand / collapse state changes
      
      extension UICollectionViewDiffableDataSource {
      
        struct SectionSnapshotHandlers<Item> {
          var shouldExpandItem: ((Item) -> Bool)?
          var willExpandItem: ((Item) -> Void)?
      	
          var shouldCollapseItem: ((Item) -> Bool)?
          var willCollapseItem: ((Item) -> Void)?
          
          var snapshotForExpandingParent: ((Item, NSDiffableDataSourceSectionSnapshot<Item>) -> NSDiffableDataSourceSectionSnapshot<Item>)?
        }
        
        var sectionSnapshotHandlers: SectionSnapshotHandlers<Item>
       
      }
    • 8:52 - Reordering Handlers

      // Diffable Data Source Reordering Handlers
      
      extension UICollectionViewDiffableDataSource {
      
        struct ReorderingHandlers {
          var canReorderItem: ((Item) -> Bool)?
          var willReorder: ((NSDiffableDataSourceTransaction<Section, Item>) -> Void)?
          var didReorder: ((NSDiffableDataSourceTransaction<Section, Item>) -> Void)?
        }
      
        var reorderingHandlers: ReorderingHandlers
      }
    • 9:45 - Diffable Data Source Transactions

      // NSDiffableDataSourceTransaction
      
      struct NSDiffableDataSourceTransaction<Section, Item> {
         var initialSnapshot: NSDiffableDataSourceSnapshot<Section, Item> { get }
         var finalSnapshot: NSDiffableDataSourceSnapshot<Section, Item> { get }
         var difference: CollectionDifference<Item> { get }
         var sectionTransactions: [NSDiffableDataSourceSectionTransaction<Section, Item>] { get }
      }
      
      struct NSDiffableDataSourceSectionTransaction<Section, Item> {
         var sectionIdentifier: Section { get }
         var initialSnapshot: NSDiffableDataSourceSectionSnapshot<Item> { get }
         var finalSnapshot: NSDiffableDataSourceSectionSnapshot<Item> { get }
         var difference: CollectionDifference<Item> { get }
      }
    • 11:07 - Diffable Data Source Reordering Example

      dataSource.reorderingHandlers.didReorder = { [weak self] transaction in 
         guard let self = self else { return }
      
         if let updateBackingStore = self.backingStore.applying(transaction.difference) {
            self.backingStore = updatedBackingStore
         }
      }

Developer Footer

  • Vídeos
  • WWDC20
  • Advances in diffable data sources
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines