我一直在尝试从我的网络服务器加载一张桌面视图。我正在异步加载它们,并在单个单元格中显示加载微调器,同时这样做。该部分工作得很好,但当我滚动和单元格刷新时,图像消失,但其他单元格数据正确加载。有趣的是,当我点击单元格时,图像突然出现!这也是正确的图像。UITableViewCell imageview在滚动时消失
任何人有一个想法是怎么回事,可能如何解决它?下面
代码:
//
// 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
使用sdwebimage(https://github.com/rs/SDWebImage)。具体请参见:将UIImageView + WebCache类别与UITableView一起使用。它处理异步加载和空白图像问题.... – Piyuesh
如何在使用SDWebImage时添加加载指示器?是否有更聪明的方式导入所有这些文件,而不是复制项目中的所有文件? –