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
- iOS tableView 异步加载图片时错位问题
- 异步加载图片错位问题
- Android ListView图片异步加载时,图片错位的问题
- Android ListView异步加载图片错位问题
- 异步加载图片错位的问题
- iOS tableView的图片缓存异步加载
- 解决图片异步加载错位
- tableview 图片异步加载
- ios UITableView 异步加载图片并防止错位
- ios UITableView 异步加载图片并防止错位
- android listview 异步加载图片时出现图片错位、刷新问题的简单解决
- android开发中解决ListView异步加载图片错位问题
- 解决Android ListView中图片异步加载错位问题
- -------------解决Android ListView中图片异步加载错位问题
- Android ListView异步加载图片错位问题解析
- Android ListView 异步加载图片错位的问题
- Android ListView 异步加载图片错位的问题
- Android ListView 异步加载图片错位的问题
- heap
- pycharm-professional-4.5.4 注册码 破解
- 第三方QQ登录、获取用户质料
- 黑马程序员——【学习笔记】多线程——线程间通讯
- Pylearn2学习(一)
- iOS tableView 异步加载图片时错位问题
- Android高效加载大图、多图解决方案,有效避免程序OOM
- layoutSubViews实践
- weblogic启动失败:Could not obtain the localhost address 解决办法
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- opencv notes
- matlab中的文件读写操作
- linux下重启weblogic(关闭和启动)nohup的使用
- iOS系统后台运行机制研究