TableView滑动不加载

来源:互联网 发布:淘宝i7主机骗术 编辑:程序博客网 时间:2024/06/07 19:22

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

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

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

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

@property (nonatomic,assign)BOOLisLoad; //是否下载

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

- (void)setValue:(id)value forUndefinedKey:(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];


NSArray *array = @[ @{@"picUrl":@"http://f.hiphotos.baidu.com/image/h%3D200/sign=04d9f5a0d7a20cf45990f9df46094b0c/d058ccbf6c81800a4483fb29b63533fa828b47b6.jpg"},
  @{@"picUrl":@"http://h.hiphotos.baidu.com/image/h%3D300/sign=9835213ebf99a90124355d362d950a58/2934349b033b5bb54eefddd431d3d539b600bce4.jpg"},
  @{@"picUrl":@"http://d.hiphotos.baidu.com/image/h%3D200/sign=dc2b8bb38526cffc762ab8b289014a7d/42166d224f4a20a42d4dc1ed97529822720ed04d.jpg"},
  @{@"picUrl":@"http://f.hiphotos.baidu.com/image/h%3D200/sign=04d9f5a0d7a20cf45990f9df46094b0c/d058ccbf6c81800a4483fb29b63533fa828b47b6.jpg"},
  @{@"picUrl":@"http://d.hiphotos.baidu.com/image/h%3D200/sign=7610960474f082023292963f7bfbfb8a/f3d3572c11dfa9ec5148243065d0f703918fc1b7.jpg"},
  @{@"picUrl":@"http://e.hiphotos.baidu.com/image/h%3D200/sign=db6484b38526cffc762ab8b289014a7d/42166d224f4a20a42a02ceed97529822720ed0f8.jpg"},
  @{@"picUrl":@"http://e.hiphotos.baidu.com/image/h%3D200/sign=bc09df35750e0cf3bff749fb3a46f23d/2fdda3cc7cd98d102db27e16263fb80e7bec90b6.jpg"},
  @{
@"picUrl":@"http://h.hiphotos.baidu.com/image/h%3D300/sign=0e698f82d643ad4bb92e40c0b2025a89/03087bf40ad162d92589103b16dfa9ec8a13cdb6.jpg"}];


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


准备好数据后,foin循环

for(NSDictionary*dicinarray) {   

    MZImageModel*model = [[MZImageModelalloc]init];

    [model setValuesForKeysWithDictionary: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;

}


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

0 0
原创粉丝点击