iOS

iOS 预加载不可见Cell

2018-01-10  本文已影响137人  887d1fc86fe6
有时候我们需要在 TableView CollectionView 这样的控件滚动之前就想要加载一些东西 我这里做的是加载图片 

我们想在当前的这些可见Cell前几个 或者后几个 的数据提前加载 这个时候就可以用到一下代码 OC的直接拷贝 我这里举例的是CollectionView 你只要把 CollectionView相关的数据源代码 换成 TableView的就可以了 获取算法还是一样的 

// 停止滚动

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView

{

// if `decelerate` was true for `scrollViewDidEndDragging:willDecelerate:`

// this will be called when the deceleration is done

[selfprefetchImagesForCollectionView:self.collectionView];

}

// 停止拖拽

- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate

{

// if `decelerate` is true, then we shouldn't start prefetching yet, because

// `cellForRowAtIndexPath` will be hard at work returning cells for the currently visible

// cells.

if(!decelerate){

[selfprefetchImagesForCollectionView:self.collectionView];

}

}

/** Prefetch a certain number of images for rows prior to and subsequent to the currently visible cells

*  

* @param tableView The tableview for which we're going to prefetch images.

*/

- (void)prefetchImagesForCollectionView:(SMCollectionView*)collectionView

{

NSArray*indexPaths = [collectionViewindexPathsForVisibleItems];

if([indexPathscount] ==0)return;

NSIndexPath*minimumIndexPath = indexPaths[0];

NSIndexPath*maximumIndexPath = [indexPathslastObject];

// they should be sorted already, but if not, update min and max accordingly

for(NSIndexPath*indexPathinindexPaths)

{

if(indexPath.section< minimumIndexPath.section|| (indexPath.section== minimumIndexPath.section&& indexPath.row< minimumIndexPath.row)) minimumIndexPath = indexPath;

if(indexPath.section> maximumIndexPath.section|| (indexPath.section== maximumIndexPath.section&& indexPath.row> maximumIndexPath.row)) maximumIndexPath = indexPath;

}

// build array of imageURLs for cells to prefetch

NSMutableArray*imageURLs = [NSMutableArrayarray];

indexPaths =[selfcollectionView:collectionViewpriorIndexPathCount:kPrefetchRowCountfromIndexPath:minimumIndexPath];

for(NSIndexPath*indexPathinindexPaths) {

[imageURLsaddObject:[selfurlForIndexPath:indexPath]];

}

indexPaths = [selfcollectionView:collectionViewnextIndexPathCount:kPrefetchRowCountfromIndexPath:maximumIndexPath];

for(NSIndexPath*indexPathinindexPaths) {

[imageURLsaddObject:[selfurlForIndexPath:indexPath]];

}

// now prefetch

if([imageURLscount] >0)

{

[[SDWebImagePrefetchersharedImagePrefetcher]prefetchURLs:imageURLs];

}

}

/** Retrieve NSIndexPath for a certain number of rows preceding particular NSIndexPath in the table view.

*

* @param tableView The tableview for which we're going to retrieve indexPaths.

* @param countThe number of rows to retrieve

* @param indexPath The indexPath where we're going to start (presumably the first visible indexPath)

*

* @returnAn array of indexPaths.

*/

- (NSArray*)collectionView:(SMCollectionView*)collectionView priorIndexPathCount:(NSInteger)count fromIndexPath:(NSIndexPath*)indexPath

{

NSMutableArray*indexPaths = [NSMutableArrayarray];

NSIntegerrow = indexPath.row;

NSIntegersection = indexPath.section;

for(NSIntegeri =0; i < count; i++) {

if(row ==0) {

if(section ==0) {

returnindexPaths;

}else{

section--;

row = [collectionViewnumberOfItemsInSection:section] -1;

}

}else{

row--;

}

[indexPathsaddObject:[NSIndexPathindexPathForRow:rowinSection:section]];

}

returnindexPaths;

}

/** Retrieve NSIndexPath for a certain number of following particular NSIndexPath in the table view.

*

* @param tableView The tableview for which we're going to retrieve indexPaths.

* @param countThe number of rows to retrieve

* @param indexPath The indexPath where we're going to start (presumably the last visible indexPath)

*

* @returnAn array of indexPaths.

*/

- (NSArray*)collectionView:(SMCollectionView*)collectionView nextIndexPathCount:(NSInteger)count fromIndexPath:(NSIndexPath*)indexPath

{

NSMutableArray*indexPaths = [NSMutableArrayarray];

NSIntegerrow = indexPath.row;

NSIntegersection = indexPath.section;

NSIntegerrowCountForSection = [collectionViewnumberOfItemsInSection:section];

for(NSIntegeri =0; i < count; i++) {

row++;

if(row == rowCountForSection) {

row =0;

section++;

if(section == [collectionViewnumberOfSections]) {

returnindexPaths;

}

rowCountForSection = [collectionViewnumberOfItemsInSection:section];

}

[indexPathsaddObject:[NSIndexPathindexPathForRow:rowinSection:section]];

}

returnindexPaths;

}

/**

*获取当前indexPath的ImageUrl

*

*@param indexPath indexPath description

*

*@return IamgeURL

*/

- (NSString*)urlForIndexPath:(NSIndexPath*)indexPath {

SMReadModel*readModel =self.dataArray[indexPath.section];

SMReadCartoonModel*readCartoonModel = readModel.readCartoonModels[indexPath.item];

returnreadCartoonModel.images;

}

上一篇下一篇

猜你喜欢

热点阅读