2009-12-03 56 views
0

我試圖將單個圖像加載到分組表格單元格。我在互聯網上找到了一段代碼並對其進行了修改。原始代碼使用UIViews來顯示圖像,這不是我所需要的,但是原始代碼當然是有效的。 PLease幫助我使這個代碼工作在一個單獨的tableview的單元格中顯示圖像。我的TableView只有一行,它有UITableViewCellStyleDefault樣式。異步圖像加載到TableView單元格

Original Code

這裏是我修改的方法,這是核心的方法。

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
{ 
    [connection release]; 
    connection=nil; 

    UIImage *img = [UIImage imageWithData:data]; 
    self.image = img; 

    self.frame = self.bounds; 
    [self setNeedsLayout]; 

    [data release]; 
    data=nil; 
} 

這就是我如何使用它在我的TableView Cell cellForRowAtIndexPath方法中顯示圖像。

CGRect frame; 
frame.size.width=75; frame.size.height=75; 
frame.origin.x=0; frame.origin.y=0; 
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
[asyncImage loadImageFromURL:imageURL]; 
cell.imageView.image = asyncImage.image; 
+0

我的評論是,只是在細胞中添加異步,而不是像這樣異步的圖像 [cell addSubview:asyncImage]; 而是這行代碼cell.imageView.image = asyncImage.image; – 2012-04-17 05:49:29

回答

0

在您發佈的代碼,該AsyncImageView的圖像不被設置,直到連接完成加載,它實際上創建了圖像後。問題是,你正在將它的圖像立即設置到單元格中。這不行!

您將要製作自定義單元類(子類UITableViewCell)並在表中使用該單元。有很多示例代碼展示瞭如何使用自定義單元格並將它們與視圖對齊。而不是使用UITableViewCell的標準imageView,創建您自己的佈局,並使用AsyncImageView來顯示您的圖像。那麼它應該工作。

+0

那麼,我知道如何使用IB創建自定義單元格,然後將其插入到我的表格行中,但是如何與使用標準UTTableViewCell方法不同呢? – 2009-12-03 18:05:48

+0

這是不同的,因爲你可以使用你的AsyncImageView類的圖像。如果您使用標準的UITableViewCell,那麼您會遇到一個普通的UIImageView。 – 2009-12-04 00:21:35

相關問題