我一直在尝试从我的网络服务器加载带有照片的表格 View 。我正在异步加载它们,并在这样做时在各个单元格中显示一个加载微调器。那部分工作得很好,但是当我滚动并且单元格被刷新时,图像消失了,但是其他单元格数据被正确加载。有趣的是,当我点击单元格时,图像突然出现了!它也是正确的图像。
有人知道发生了什么事以及可能如何解决它吗?
代码如下:
//
// GalleryViewController.m
// Photo
//
// Created by Thomas on 5/8/14.
// Copyright (c) 2014 Teilmann. All rights reserved.
//
#import "GalleryViewController.h"
#import "DBHandler.h"
#import "PListHandler.h"
@interface GalleryViewController ()
@end
@implementation GalleryViewController
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
@synthesize nsarray, gallery;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed: 0.9333 green:0.3451 blue:0.08234 alpha:1.0];
PListHandler *userinfo = [[PListHandler alloc] initWithPlistID:@"userinfo"];
_dbhandler = [[DBHandler alloc] init];
NSMutableArray *test = [_dbhandler getGalleryByUserID:[userinfo getUserID]];
gallery = [NSArray arrayWithArray:test];
NSLog(@"test: %@", test);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [gallery count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"My Gallery";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"defaultcell" forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultcell"];
}
cell.imageView.image = nil;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(160, 47);
[spinner startAnimating];
[cell.contentView addSubview:spinner];
//hide labels until done loading
cell.textLabel.hidden = YES;
cell.detailTextLabel.hidden = YES;
//download cell content async and display loading indicator in cells.
dispatch_async(kBgQueue, ^{
NSString *profilePicName = [NSString stringWithFormat:@"%@%@", [self.dbhandler getPicturesPath], [[gallery objectAtIndex:indexPath.row] valueForKey: @"filename"]];
NSURL *profilepicurl = [NSURL URLWithString:profilePicName];
NSData *imgData = [NSData dataWithContentsOfURL:profilepicurl];
if(imgData){
UIImage *icon = [UIImage imageWithData: imgData];
if(icon){
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if(updateCell){
NSLog(@"hej");
[updateCell.imageView setImage: icon];
updateCell.imageView.hidden = NO;
NSString *subtitle = [NSString stringWithFormat:@"Comments: %@ \nPosted: %@", [[gallery objectAtIndex:indexPath.row] valueForKey:@"comments"], [[gallery objectAtIndex:indexPath.row] valueForKey:@"created_at"]];
cell.detailTextLabel.numberOfLines = 0;
cell.textLabel.text = [NSString stringWithFormat:@"Votes: %@",[[gallery objectAtIndex:indexPath.row] valueForKey:@"votes"]];
cell.detailTextLabel.text = subtitle;
//Stop spinner and make labels visible
[spinner stopAnimating];
cell.textLabel.hidden = NO;
cell.detailTextLabel.hidden = NO;
}
});
}
}
});
return cell;
}
@end
最佳答案
我认为你的问题是每次显示单元格时你都在加载图像,请记住,你正在重用单元格 View 。
我用过 https://github.com/rs/SDWebImage它有一种方法来缓存图像并显示占位符。
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
关于ios - UITableViewCell imageview 滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613186/