TableView滑动不加载

滑动不加载,在ios中非常常用,是优化界面的一种方法,今天给大家分享一下,废话不多说,直接上代码!!

第一步,先在controller中添加一个tableView并实现其代理方法。

第二步,创建一个模型,声明两个属性,

@property(nonatomic,copy)NSString*picUrl; //图片

@property(nonatomic,assign)BOOLisLoad;

//是否下载

千万别忘了在.m文件中写上:

- (void)setValue:(id)valueforUndefinedKey:(NSString*)key{

}//防止崩溃

第三步,重写Cell在.h中声明一个方法:

- (void)setImageWithModel:(MZImageModel*)model;

在.m中写实现方法

- (void)setImageWithModel:(MZImageModel*)model

{

if(model ==nil){ //如果模型为空,加载占位图

[self.mainImagesetImage:[UIImageimageNamed:@"14.png"]];

}else{//否则,请求图片

[self.mainImagesd_setImageWithURL:[NSURLURLWithString:model.picUrl]placeholderImage:[UIImageimageNamed:@"14.png"]completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {

[modelsetIsLoad:YES];//下载

}];

}

}

最后回到Controller中,先声明一个全局的数组并初始化:

_data=

[[NSMutableArrayalloc]init];

因为是个人练习,所用的都是假数据,这个大家就八仙过海吧。

准备好数据后,foin循环

for(NSDictionary*dicinarray) {

MZImageModel*model = [[MZImageModelalloc]init];

[modelsetValuesForKeysWithDictionary:dic];

[modelsetIsLoad:NO];//装到数组中时,先不下载。

[_dataaddObject:model];

}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,

(int64_t)(3*NSEC_PER_SEC)),dispatch_get_main_queue(),

^{//让tableView准备好后,再显示

[selfloadShowCells];

});

//在这里要说一下,我用的是sdWebImage第三方请求的图片,它的先 从缓存。沙盒。磁盘三级缓存,都没有才从网络请求

//加载可见cell图片是请求数据的

- (void)loadShowCells

{

NSArray*array

= [self.tableindexPathsForVisibleRows];//TableView中,想要什么,就以什么开头

for(NSIndexPath*indexPathinarray) {

//1.获取cell

MZImageCell*cell = [self.tablecellForRowAtIndexPath:indexPath];

MZImageModel*model =_data[indexPath.row];

[cellsetImageWithModel:model];

}

}

这里还用到了scroller的两个方法

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView

{

[selfloadShowCells];//这个方法是,停止滑动时开始加载。

}

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

{

if(!decelerate) { //滑动时,加载占位图

[selfloadShowCells];

}

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

return_data.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

staticNSString*identifier =@"image";

MZImageCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

if(!cell) {

cell =[[NSBundlemainBundle]loadNibNamed:@"MZImageCell"owner:selfoptions:nil].lastObject;

}

//对cell进行判断

MZImageModel*model =_data[indexPath.row];

if(model.isLoad){

//此时执行这个方法是从缓存中提取的

[cellsetImageWithModel:model];

}else{

//显示占位图

[cellsetImageWithModel:nil];

}

returncell;

}

有啥不对的地方还请大神们,多多指点!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,366评论 30 472
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,959评论 3 38
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 8,927评论 2 4
  • UITableViewCell 高度计算 UITableView 询问 cell 高度有两种方式:1.rowHei...
    WeiHing阅读 9,907评论 6 16
  • 太阳每天都会照常升起,当我们一边唠叨着要享受生活,一边又为了生活而不停的工作,一边感叹时间一去不复返的无奈,一边又...
    诗画言熙阅读 3,328评论 0 0