2014-11-03 41 views
0

我想知道什麼是從服務器設置我的UITableView圖像的最佳方式。我在亞馬遜S3存儲圖像,並獲得我在後臺實現邏輯的圖像,需要兩種說法:如何設置UITableViewCell imageView與從服務器獲取的圖像 - 雙重請求

  • 我使用來獲得圖像的URL第一個和第二個我只是單純地顯示圖像到UITableViewCell。對於Web請求,我使用AFNetworking庫和UIImageView (<UIImageView+AFNetworking.h>)的補充。我對請求沒有任何問題,但是我不太確定我應該如何讓我的代碼負責獲取所有圖像的URL。在我看來,cellForARowAtIndexPath方法僅適用於顯示來自最終url地址的圖像:

這是我的代碼。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"final url here"]]; 
[cell.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"GeofencingAlert.png"] 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage 
*image) 
{ 

} 
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    NSLog(@"Error: %@",error.description); 
}]; 

我應該首先下載所有存儲在Amazon S3上的網址,並把它們放進NSMutuableArray?如果是,什麼是執行多個請求的最佳方式?如果用戶向表中添加新記錄怎麼辦?我是否必須再次執行多個請求才能更新我的UITableVIewCell圖像?提前致謝。

回答

0

您應該使用-(void)viewDidLoad方法。像這樣的東西

- (void)viewDidLoad 
{ 
    urlPaths = nil; 
    [httpClient loadUrlsWithSuccess:^(NSArray *results){ 
     urlPaths = results; // urlPaths - class member 
     [tableView reloadData]; 
    }]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath 
{ 
    //... 
    if(urlPaths) 
    { 
    NSString *urlPath = urlPaths[indexPath.row]; 
    // load async 
    } 
    else 
    // urls are not loaded yet 
    imageView.image = placeholderImage; 
}