我一直在尝试从我的网络服务器加载带有照片的tableview。我正在异步加载它们并在单个单元格中显示加载微调器。该部分工作得很好,但是当我滚动并且单元格被刷新时,图像会消失,但其他单元格数据会正确加载。有趣的是,当我点击单元格时,图像突然出现!它也是正确的图像。
任何人都有一个想法,可能会如何修复它?
以下代码:
//
// 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
答案 0 :(得分:1)
我认为您的问题是,每次显示单元格时都会加载图片,请记住,您正在重复使用单元格视图。
我已经使用https://github.com/rs/SDWebImage它有一种方法来缓存图像并显示一个占位符。
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
答案 1 :(得分:0)
"细胞"您的tableView
中可以重复使用。滚动时,imageView.image
设置为nil
,实际上您的图像会消失。您必须将图像存储在其他位置。
可能在由NSDictionary
键入的indexPath
中。在那里你应该在加载后设置图像,而不是将单元格的图像设置为nil
,从字典中检索图像并将其设置在单元格imageView
上。