【发布时间】:2015-10-22 17:46:43
【问题描述】:
我的自定义单元格代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageView"];
NSURL *nsurl = [NSURL URLWithString:_imageUrls[indexPath.row]];
NSData *data = [NSData dataWithContentsOfURL: nsurl];
cell.imageView.image = [UIImage imageWithData:data];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImageView"];
}
return cell;
我有自定义类 VKTableViewCell:
@interface VKTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
图片约为 800x800(从网站解析)。 当我滚动我的表格时,内存在增长(从 20 到 30mb,加载 100 张图像)。为什么会这样?在这种情况下我必须做什么?我正在使用 ARC。
【问题讨论】:
-
虽然与此问题无关,但您可能应该在创建新单元格的调用之后调用设置图像。 (在 dequeReusableCellWithIdentifier 返回 nil 的情况下。
-
你所做的一切都是错误的。您不得在
cellForRow中同步使用网络。您必须立即返回单元格。用户正在现在滚动。 现在返回一个单元格。 -
@matt 我还知道服务质量和线程。这不是最后一个版本的代码。但是假设是主线程,内存泄漏正常吗?
-
每次我们看到相同的单元格时,您都在下载图像(例如,上下滚动和上下滚动)。那是疯癫。 将图像存储在数据模型中。你所做的一切都是错误的。 (我想我是这么说的。)
-
@matt 谢谢,我会做的
标签: ios uitableview custom-cell