iOS tableView 异步加载图片时错位问题

来源:互联网 发布:管理学培训课程网络 编辑:程序博客网 时间:2024/05/18 17:23

写了一个图片异步下载加载任务的组件,程度很简单。

根据测试,发现这个设计很有实用价值,然后到tableview上测试发现一塌糊涂。

首先,发现由于异步加载、下载图片,导致了开始的时候显示不了图片,移动则是正常,接着发现图片加载时有错位现象,谷歌在家用不了,就bing过,网上的建议也是好像不怎么好使,于是,根据逻辑来测试,发现不错位的方案。


首先这篇文章是采用本人写的图片异步下载加载任务组件,但关系不大,只是异步处理的事情。

首先,贴上代码上的笔记。记录的比较好。


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.dataSource.count;}- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"aaaa"];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaaa"];    }        //可有可无,有的话,会比较美观。尤其是最初没有数据的时候。    cell.imageView.image = [UIImage imageNamed:@"custom.png"];            ImageAsyncTaskObject* aObject = [ImageAsyncTaskObject CreateWithImageURLString:self.dataSource[indexPath.row]];        //为什么不采用Block方式,而采用delegate方式呢?    //原因是退出本ViewController时,ImageAsyncTaskObject实例还没有完成异步处理,导致还在进行引用Block。    //即使本ViewController看似是退出了,但实质却还没有释放本ViewController,还没有执行到dealloc的方法。    //必须等候所有ImageAsyncTaskObject实例异步处理完,释放完,本ViewController才会真正被释放,执行dealloc。    aObject.taskDelegate = self;        //记录当前Cell 的 indexPath 值,方便 delegate的时候,找寻出对应的Cell。    aObject.indexPath = indexPath;        return cell;}- (void) imageAsyncTaskObjectDidFinishAsyncTask:(ImageAsyncTaskObject *)aTaskObject{    //别直接使用Cell 对 imageView 进行赋值,会错位的。    //通过indexPath 获取 Cell 再对 imageView 进行赋值是不会错位的。    UITableViewCell* tempCell = [self.myTableView cellForRowAtIndexPath:aTaskObject.indexPath];    tempCell.imageView.image = aTaskObject.image;}


看看上面的注释的话,应该很好的理解了。




下面就记录一下我的组件的头文件。仅供欣赏。呵呵。

////  ImageAsyncTask.h//  ABCAAAA////  Created by yococo on 15/10/9.//  Copyright (c) 2015年 yococo. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@class ImageAsyncTaskObject;@protocol ImageAsyncTaskObjectDelegate;@interface ImageAsyncTask : NSObject{    NSMutableArray* taskObjectPool;    NSMutableDictionary* taskObjectDictionary;}+ (ImageAsyncTask *)sharedInstance;- (BOOL) addObject:(ImageAsyncTaskObject*)aObject;- (void) removeObject:(ImageAsyncTaskObject*)aObject;@property(nonatomic,readonly) NSArray* taskArray;@end@interface ImageAsyncTaskObject : NSObject@property(nonatomic,weak) id<ImageAsyncTaskObjectDelegate> taskDelegate;@property(nonatomic,strong) NSString* imageURLString;@property(nonatomic,strong,readonly) UIImage* image;//初始化的时候会自动将 ImageAsyncTaskObject 加入 ImageAsyncTask里面//完成下载后,ImageAsyncTaskObject 会自动从 ImageAsyncTask里面移除- (id) initWithImageURLString:(NSString*)aImageURLString;+ (id) CreateWithImageURLString:(NSString*)aImageURLString delegate:(id<ImageAsyncTaskObjectDelegate>)delegate;+ (id) CreateWithImageURLString:(NSString*)aImageURLString;@property(nonatomic,weak) NSIndexPath *indexPath;@end@protocol ImageAsyncTaskObjectDelegate <NSObject>@optional- (void) imageAsyncTaskObjectDidFinishAsyncTask:(ImageAsyncTaskObject *)aTaskObject;@end

匆忙之际写下的,有待今后的项目使用时改善。


从 22 : 27 开始在群里说要做这个组件,到  22 : 48 完成了测试版,再到 23 : 41 完成了定稿版......随后在群里扯淡,1 : 00 的时候测试发现Bug问题,就是本文开头所说的,2 : 00 的时候完成了已知Bug的修改,便到博客记录。


0 0
原创粉丝点击