UICollectionView Prefetch Data Source in iOS 10?

这一生的挚爱 提交于 2019-12-04 02:31:25
Milap Kundalia

UICollectionView gains a new property this year called prefetchDataSource. Just like the existing delegate and dataSource properties, we can simply set it to some object that implements the new UICollectionViewDataSourcePrefetching protocol.

This protocol is brand new in iOS 10, and requires we implement just one new function:

public func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath])

When this function is called, we can examine the indexPaths array we're passed in to know which cells are "coming up soon" and thus which cells we should probably begin loading the data for.

For detail understanding with example refer link UICollectionViewDataSourcePrefetching

i was had the same problem after updating from xcode 8 to 8.1. I could solve it deleting the (Outlets reference) from the main.storyboard.

This for every CollectionView that i had in the storyboard. Also leave my ViewController just like that:

Class (YourViewControllerName): (YourViewControllerType) , UICollectionViewDataSource, UICollectionViewDelegate{

}

Then i can build and run my App

However, if you need use (UICollectionViewDataSourcePrefetching) i recomment you that delete the current UICollectionView, later create a new UICollectioView. just like that you can use UICollectionViewDataSourcePrefetching after updateing xcode or swift

Implement protocol "UICollectionViewDataSourcePrefetching" in you ViewController as

class ViewController: UIViewController , UICollectionViewDataSourcePrefetching {

Set following delegates to your collection view in storyboard (see the attached image) or programmatically

In ViewController's viewDidLoad method

collectionView.delegate = self

collectionView.dataSource = self

collectionView.prefetchDataSource = self

Refer this example - https://github.com/Be-With-Viresh/CollectionViewWithPrefetch

Devil

Add protocol "UICollectionViewDataSourcePrefetching" And then use below functions.

  1. collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath])

  2. collectionView(_ collectionView: UICollectionView, cancelPrefetchingForItemsAt indexPaths: [IndexPath])

For detail refer Link: https://adoptioncurve.net/archives/2016/06/collection-view-updates-in-ios10/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!