异步下载图片,动态设置cell高度
来源:互联网 发布:mac开机进入客人用户 编辑:程序博客网 时间:2024/05/22 18:23
在网上找到了一段不错的代码,笔记一下
- #pragma mark - UITableViewDelegate, UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.imgArray.count;//图片URL以数组的形式存在
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- // 先从缓存中查找图片
- UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: self.imgArray[indexPath.row]];
- // 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。
- if (!image) {
- image = [UIImage imageNamed:kDownloadImageHolder];
- }
- //手动计算cell
- CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
- return imgHeight;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *imgID = @"pictureCellID";
- PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];
- if (nil == cell) {
- cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];
- }
- [self configureCell:cell atIndexPath:indexPath];
- cell.userInteractionEnabled = NO;
- return cell;
- }
- - (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
- NSString *imgURL = self.imgArray[indexPath.row];
- UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];
- if ( !cachedImage ) {
- [self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];
- [cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];
- } else {
- [cell.btn setBackgroundImage:cachedImage forState:UIControlStateNormal];
- }
- }
- - (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath {
- // 利用 SDWebImage 框架提供的功能下载图片
- [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
- // do nothing
- } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
- [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.tableView reloadData];
- });
- }];
- }
0 0
- 异步下载图片,动态设置cell高度
- 动态设置Cell的高度
- ios 动态设置Cell的高度
- UITableView 异步加载图片,cell自适应高度!项目常用。
- 动态计算cell高度
- 动态获取cell高度
- Cell高度的设置
- 几种设置UITableView的cell动态高度的方法
- 动态设置UITableView的cell高度(old)
- 关于Xib使用AutoLayout动态设置cell高度
- 最简单动态设置不同cell高度的方法。
- 点击cell动态改变cell高度
- Android 根据图片高度动态设置ViewPager高度
- 动态计算Cell的高度
- 动态计算cell得高度
- 动态调整cell的高度
- UITableView动态改变Cell高度
- IOS动态获取cell高度
- 读取类所在目录路径
- 8.Decorator-装饰模式
- 9.Proxy-代理模式
- Mybits入门--配置文件使用
- 信息安全工程师 学习笔记(十三)
- 异步下载图片,动态设置cell高度
- PHP--笔试题
- 10.Facade-外观模式
- Jenkins进阶系列之——14配置Jenkins用户和权限
- 11.Composite-组合模式
- Coursera机器学习(Andrew Ng)笔记:大规模机器学习
- 12.Bridge-桥接模式
- HEX----组合数+逆元+思维 山东省第八届省赛D题
- 开发人员的必备技能