2014-05-12 59 views
0

我一直在嘗試從我的網絡服務器加載一張桌面視圖。我正在異步加載它們,並在單個單元格中顯示加載微調器,同時這樣做。該部分工作得很好,但當我滾動和單元格刷新時,圖像消失,但其他單元格數據正確加載。有趣的是,當我點擊單元格時,圖像突然出現!這也是正確的圖像。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 
+0

使用sdwebimage(https://github.com/rs/SDWebImage)。具體請參見:將UIImageView + WebCache類別與UITableView一起使用。它處理異步加載和空白圖像問題.... – Piyuesh

+0

如何在使用SDWebImage時添加加載指示器?是否有更聰明的方式導入所有這些文件,而不是複製項目中的所有文件? –

回答

1

我認爲你的問題是每次顯示單元格時都要加載圖像,請記住,你正在重複使用單元格視圖。

我用https://github.com/rs/SDWebImage它緩存圖像,並顯示一個佔位符的方法。

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
0

「細胞」 在你的tableView是可重複使用。當您滾動imageView.image設置爲nil時,圖像消失。你必須把你的圖像存儲在其他地方。

也許在NSDictionary,這是由indexPath鍵。在加載它之後,您應該設置圖像,而不是將單元格的圖像設置爲nil,從字典中檢索圖像並將其設置在單元格imageView上。

+0

在我的代碼中,我應該在字典中存儲圖像嗎?在我的tableview callForRowAtIndexPath異步調用? –