Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.81 KB

data-source.md

File metadata and controls

60 lines (46 loc) · 1.81 KB

Using the data source

Let’s start by defining an array that contains the information we need to display our menu items:

class ViewController: UIViewController {
    let cities = [
        "Oslo",
        "Stockholm",
        "Tokyo",
        "Barcelona",
        "Vancouver",
        "Berlin"
    ]
}

Then we initialize our PagingViewController:

class ViewController: UIViewController {
    ...
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let pagingViewController = PagingViewController()
        pagingViewController.dataSource = self
    }
}

In our data source implementation we set the number of view controllers equal to the number of items in our cities array, and return an instance of PagingIndexItem with the title of each city:

extension ViewController: PagingViewControllerDataSource {
    func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
        return cities.count
    }

    func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
        return CityViewController(city: cities[index])
    }

    func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
        return PagingIndexItem(index: index, title: cities[index])
    }
}

The viewControllerForIndex method will only be called for the currently selected item and any of its siblings. This means that we only allocate the view controllers that are actually needed at any given point.

Parchment will automatically set the first item as selected, but if you want to select another you can do it like this:

pagingViewController.select(index: 3)

This can be called both before and after the view has appeared.

Check out the DelegateExample target for more details.