iOS 预加载不可见Cell

简介: iOS 预加载不可见Cell

``` 有时候我们需要在 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;


}


```


相关文章
|
6月前
|
iOS开发
iOS UITableViewCell刷新某些行的cell或section
iOS UITableViewCell刷新某些行的cell或section
76 0
|
JSON iOS开发 数据格式
iOS开发-图文混排之cell自适应
iOS开发-图文混排之cell自适应
103 0
iOS开发-图文混排之cell自适应
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
366 0
|
iOS开发 开发者
iOS开发-简述UITableView中cell的重用问题
iOS开发-简述UITableView中cell的重用问题
189 0
|
iOS开发
ios 添加到cell 上的button点击无效!扩大button的点击区域(黑魔法)
一般情况下点击效果都是正常的!要不然你对它做了什么?一般细心的小伙伴都没有遇到这种情况,但是呢! 当然我是二班的!在这里我主要讲两个问题,解决问题和普及魔法。 一.普及问题(button在cell上点击无效) 自定义一个cell,cell里边creat了一个button!然后调试了半天,什么反应都没有!   1.button的enable 设置为yes可点击的。
1491 0
|
iOS开发 API
ios整理(六)关于用富文本在tableview的cell去加载html字符串的优化方案
ios整理(六)关于用富文本在tableview的cell去加载html字符串的优化方案1.相信用iOS系统的类去加载html字符串很多人第一反应就是 NSString *contens = @"1231我给你数点这里";NSData *data = [contens dataUsingEncod...
2258 0
|
iOS开发
iOS学习笔记--tableView中如何获取cell上textfiled的值
iOS学习笔记--tableView中如何获取cell上textfiled的值 最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。
1480 0