2012-11-06 123 views
1

我會解釋我的問題,希望能找到解決辦法。我正在尋找幾周來解決這個問題,但是找不到如何去做,不想使用外部庫或稀有類。TableView與異步加載遠程圖像

說明:

我有一個TableView中從XML自動獲得的信息,該XML我的全部被解析並存儲在MutableArray的信息。

該數組解析我所有的XML標籤,它工作正常。

要讀取一個標籤我做我的解析器如下:

//要使用這個標籤標題:

[self.parseResults objectAtIndex:indexPath.row] objectForKey:@ 「name_category_ads」];

//要使用該圖像標籤:

[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@ 「image1_category_ads」];

問題:

的TableView中文本正確加載我的陣列(標題),但我不能擁有它加載從我的陣列中的圖像的tableview:

我測試我的代碼以靜態URL圖像(一個圖像在網站上)和異步加載,如果它的工作,但不工作,當我嘗試從我的數組,它具有不同的圖像網址。

//圖像URL靜態

NSString *[email protected]"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg"; 

的代碼爲我的表中的實施例是如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

//My custom cell 

Cell_Category_Iphone *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[Cell_Category_Iphone alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

//Image Static URL 
NSString *[email protected]"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg"; 

//Assign the title to my cell, using my array already loaded and parsed. 

cell.titleCategoryCell.text=[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"name_category_ads"]; 

//Asynchronous loading of image 

if (!cell.imageCategoryCell.image) { 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 

NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:imagenURL]]; 

dispatch_sync(dispatch_get_main_queue(), ^{ 
     UIImage *thumbnail = [UIImage imageWithData:data]; 
     cell.imageCategoryCell.image=thumbnail; 
     [self.tableView beginUpdates]; 
     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 
    }); 
}); 
} 

return cell; 
} 

四捨五入了問題,其中包括:

的代碼加載正確的圖像asicronica,但在URL中的單個圖像,但我不能讓它加載我的數組中的圖像。

//我的陣列圖像

[[self.parseResults objectAtIndex: indexPath.row] objectForKey: @ "image1_category_ads"]; 

你能幫助我嗎?然後分享給大家的代碼。 非常感謝

問候當你正在使用的AFNetworking library有一個非常好的實施裏卡多

回答

2

。它是UIImageView的一個擴展,它爲您處理異步加載。你只需要設置URL,這是一個單線程。看看UIImageView+AFNetworking at GitHub。如果您不想使用AFNetworking庫,您可能需要have a look at this sample。這是非常基本的,也是一樣的,但只有一個簡單的課程。

+0

嗨,感謝您的回覆。 這兩個都不適合我,第一個非常複雜,第二個沒有得到它的工作。 任何其他幫助? 謝謝 –

+0

你好,實現解決!非常感謝你 –

0

設法解決問題。我所做的就是驗證並消除圖像陣列,空白區域和換行符,並且完美運行。 問候 裏卡多