A Swift utility to make updating table views/collection views trivially easy and reliable.

Related tags

Layout ArrayDiff
Overview

ArrayDiff Carthage compatible Pods Pod platforms

An efficient Swift utility to compute the difference between two arrays. Get the removedIndexes and insertedIndexes and pass them directly along to UITableView or UICollectionView when you update your data! The diffing algorithm is the same that powers the diff utility – it's robust and quick.

Basic Example

let old = [ "a", "b", "c", "d" ]
let new = [ "a", "c", "e", "f", "d" ]

let diff = old.diff(new)
// diff.removedIndexes = { 1 }
// diff.insertedIndexes = { 2, 3 }

// To update rows in section 0:
tableView.beginUpdates()
self.data = new
diff.applyToRowsInTableView(tableView, section: 0, rowAnimation: .Automatic)
tableView.endUpdates()

// Or to update sections:
tableView.beginUpdates()
self.data = new
diff.applyToSectionsInTableView(tableView, rowAnimation: .Automatic)
tableView.endUpdates()

Nested Diff

You can use types that conform to SectionType to perform nested row- and section-level diffs simultaneously:

let old = [
  BasicSection(name: "Alpha", items: ["a", "b", "c", "d", "e"]),
  BasicSection(name: "Bravo", items: ["f", "g", "h", "i", "j"]),
  BasicSection(name: "Charlie", items: ["k", "l", "m", "n", "o"])
]
let new = [
  BasicSection(name: "Alpha", items: ["a", "b", "d", "e", "x"]),
  BasicSection(name: "Charlie", items: ["f", "g", "h", "i", "j"]),
  BasicSection(name: "Delta", items: ["f", "g", "h", "i", "j"])
]

let nestedDiff = old.diffNested(new)
// nestedDiff.sectionsDiff.removedIndexes == {1}
// nestedDiff.sectionsDiff.insertedIndexes == {2}
// nestedDiff.itemDiffs[0].removedIndexes == {2}
// nestedDiff.itemDiffs[0].insertedIndexes == {5}
// etc.

tableView.beginUpdates()
self.data = new
nestedDiff.applyToTableView(tableView, rowAnimation: .Automatic)
tableView.endUpdates()

Limitations

Item moves are treated as remove/insert, so when they are animated the cell will "teleport" to its new position, rather than sliding there. If you would like this feature, let me know in the Issues!

Example Project

Check out the iOS app in the Example folder to see this framework pushed to its limits to drive a UITableView. In it we have a table view with 20 sections of strings. When you tap update, the data is randomly updated and we assert that the changes we made are equal to the changes that the framework recovers by comparing the two arrays.

Attribution

Thanks to https://github.com/khanlou/NSArray-LongestCommonSubsequence which I took inspiration totally copied from.

You might also like...
Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only

SecureView Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only Usage UIWindow* mainWindow; - (void) setup {

This library allows you to make any UIView tap-able like a UIButton.

UIView-TapListnerCallback Example To run the example project, clone the repo, and run pod install from the Example directory first. Installation UIVie

IOS-PokemonQuizApp - Assignment to make a responsive iOS app. App has to connect with an external API

iOS-PokemonQuizApp Assignment to make a responsive iOS app. App has to connect with an external API. The Project The idea of the project is to make a

Make the web shine. ✨

Sparkle Make the web shine. ✨ Note that the software provided in this package is still in early development, and APIs are subject to changes. Static S

A collection of operators and utilities that simplify iOS layout code.

Anchorage A lightweight collection of intuitive operators and utilities that simplify Auto Layout code. Anchorage is built directly on top of the NSLa

Horizontal and Vertical collection view for infinite scrolling that was designed to be used in SwiftUI
Horizontal and Vertical collection view for infinite scrolling that was designed to be used in SwiftUI

InfiniteScroller Example struct ContentView: View { @State var selected: Int = 1 var body: some View { InfiniteScroller(direction: .ve

swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community
swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community

swiftUIViews | 👋 swiftUIviews is an online collection of beautifly designed swiftUIViews by the swift community. Feelin like contributing? Follow the

TicTacToe Game Collection View With Swift

TicTacToe---Collection-View Game Rules A game will consist of a sequence of the following actions: Initially, the "X" marks will play first (we call h

A Code challenge I solved leveraging a lot on Composite collection view layout written in swift

AsthmApp Mobile app designed as a support aid for people with Asthma Accounts Google and Firebase [email protected] dICerytiMPSI Facebook asthmp.ap

Comments
  • Fixed the warning generated by Swift 2.2

    Fixed the warning generated by Swift 2.2

    I only bumped the minor version of the pod, but given that the changes are breaking for anyone < Swift 2.2 we can bump the major version to cover anyone who hasn't migrated to 2.2 yet.

    opened by SudeepSidhu 4
  • Move Items like Doppelganger

    Move Items like Doppelganger

    Doppelganger (https://github.com/Wondermall/Doppelganger) is able to move the items. It would be nice to move items instead of remove and re adding them

    opened by mRs- 0
  • Migrate to Swift 2.3 and Swift 3

    Migrate to Swift 2.3 and Swift 3

    In order to migrate and test our app to Swift 2.3/3.0 prior to the release of Xcode 8 and iOS 10, all of our dependencies need to be migrated first.

    This can be solved by pointing our podfile to those respective Swift 2.3/3.0 branches, but this repo does not appear to have either of those branches.

    This blog post contains helpful tips for migrating open source libraries.

    opened by suchtomwow 0
Owner
Adlai Holler
Ex-google farmer/bitcoin bull
Adlai Holler
Simple static table views for iOS in Swift.

Simple static table views for iOS in Swift. Static's goal is to separate model data from presentation. Rows and Sections are your “view models” for yo

Venmo 1.3k Jan 5, 2023
Swift-picker-views - inline single and multi picker views for UIKit. Without tableview! Easy and simple

swift-picker-views Inline single and multiple picker views for UIKit. No tablevi

IBRAHIM YILMAZ 2 Jan 31, 2022
Modern-collection-view - Modern collection view for swift

Modern collection view Sample application demonstrating the use of collection vi

Nitanta Adhikari 1 Jan 24, 2022
Reusable GridView with excellent performance and customization that can be time table, spreadsheet, paging and more.

GridView GridView can tile the view while reusing it. It has an API like UIKit that works fast. Even when device rotates it smoothly relayout. Appetiz

Kyohei Ito 830 Dec 23, 2022
Reframing SwiftUI Views. A collection of tools to help with layout.

Overview A Swift Package with a collection of SwiftUI framing views and tools to help with layout. Size readers like WidthReader, HeightReader, and on

Ryan Lintott 84 Dec 16, 2022
✂ Easy to use and flexible library for manually laying out views and layers for iOS and tvOS. Supports AsyncDisplayKit.

ManualLayout Table of Contents Installation Usage API Cheat Sheet Installation Carthage Add the following line to your Cartfile. github "isair/ManualL

Baris Sencan 280 Sep 29, 2022
A utility application to capture and review search results from Swift Package Index.

SPISearch An app (macOS & iOS) to explore the search results from Swift Package Index. Testflight Links: SPIIndex (iOS and macOS apps) Search Ranking

Joseph Heck 5 Jul 26, 2022
A AutoLayout Utility for iOS

QLayout is an Utility to make Auto Layout easy on iOS. Contents Requirements Installation Usage Credits License Requirements iOS 8.0+ Swift 3.0+ Insta

Jose Quintero 3 Nov 14, 2021
Make your notification banners smaller and add some color to them

Liddell Liddell notification banners Installation Add this repository to your Package Manager: https://repo.litten.love Install Liddell Compiling Depe

alexandra 34 Dec 22, 2022
LoadingButtton - Add button extendded from LoadingButton in the view and make it center horizontally

LoadingButtton Usage/Examples Add button extendded from LoadingButton in the vie

Alireza 1 Jan 8, 2022