A Swift data structure for easier pagination
PagedArray is a generic Swift data structure for helping you implement paging mechanisms in (but not limited to) UITableViews, UICollectionViews and UIPageViewControllers.
It is a Swift-version of its Objective-C equivalent AWPagedArray
, which was used for implementing the techniques described in the blog post Fluent Pagination – no more jumpy scrolling.
PagedArray represents a list of optional elements stored by pages. It implements all the familiar Swift collection protocols so your datasource can use it just like a regular Array while providing an easy-to-use API for setting pages of data as they are retrieved.
// Initialize
var pagedArray = PagedArray<String>(count: 200, pageSize: 20)
// Set data pages
pagedArray.set(elements: ["A" ... "T"], forPage: 1)
// Retrieve data like a normal array containing optional elements
pagedArray.count // 200
pagedArray[0] // "A"
pagedArray[100] // nil
// Iterate
for element: String? in pagedArray {
// Do magic
}
// Map, filter reduce
pagedArray.filter{ $0 != nil }.map{ $0! }.reduce("", combine:+) // "ABCDE..."
// Convert to array
Array(pagedArray) // ["A", "B", ... nil, nil]
Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.
To use PagedArray with a project targeting iOS 7, you must include the PagedArray.swift
source file directly in your project.
To integrate PagedArray into your project using SwiftPM add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/MrAlek/PagedArray", from: "0.9"),
],
To integrate PagedArray into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'PagedArray'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate PagedArray into your Xcode project using Carthage, specify it in your Cartfile
:
github "MrAlek/PagedArray"
The included example project shows a demo with a tweakable UITableViewController
displaying a large number of rows using paging data.
The data structure is thoroughly tested by included XCUnit tests but no guarantees are given.
PagedArray is available under the MIT license. See the LICENSE file for more info.