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.setElements(["A" ... "T"], pageIndex: 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]
PagedArray is currently built for Swift 1.2 using Xcode 6.3.
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 Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'PagedArray', '~> 0.2'
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" >= 0.2
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.