2012-04-10 84 views
7

我在運行時獲取圖像的URL,並且我想要下載&將這些圖像顯示在表格中。圖像將被異步下載。更重要的是,我想用他們的實際尺寸顯示所有這些圖像。根據下載的圖像高度調整表格單元高度

請幫助我。在此先感謝:)

+0

幾個問題的亞人。這些圖像會佔用完整的320px寬度的屏幕嗎?或者不管寬度如何,它們都會顯示在另一個下面?或者,如果前一幅圖像的寬度小於320,並且下一幅圖像也可以適合其餘寬度(320-prev_img_width),則會出現在前一幅圖像旁邊或前一幅圖像下方。 – 2013-03-01 19:05:57

回答

2

退房heightForRowAtIndexPath:。這是關於它的另一個post

1

爲u必須創建自定義的UITableViewCell和U可以使用此代碼

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int rowHeight =0.0f; 

    // here u can get particular Url by indexPath.row, now create UIImage object using that Url 

    CGRect bioHeadingFrame = image.frame; 
    rowHeight = size.height; //get the height of that image 

    return rowHeight; 
} 

現在你的表格單元格的高度根據圖像高度

+1

你說的是對的。但要下載並顯示錶格視圖單元格中的圖像,我正在使用Asynchronous View類。我將圖像URL傳遞給此類(異步視圖類)以進行下載。我只有在下載圖像後才能得到實際的圖像大小。那麼,我怎麼能告訴tableview重新加載和製作圖像大小的各個單元格。 – rahul 2012-04-11 06:36:18

+1

只是搜索SO:http://stackoverflow.com/a/4448393/207616 – 2013-03-01 19:03:27

+1

relikd是正確的。 reloadRowsAtIndexPaths是關鍵。 – 2013-03-01 19:13:44

0

如果是增加設置表視圖單元格的大小異步下載是你真正的問題,請參考AFNetworking庫的UIImageView類別。尤其是此功能:

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage 

您可以將此UIimageview類別包含到您的項目中,並設置從該類派生的細胞圖像。

3

在下載完就可以使用

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
       withRowAnimation:UITableViewRowAnimationAutomatic]; 

這將再次調用

-(CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath 

何時更新圖像委託方法所以,你應該使用圖像的高度,如果它存在,默認高度,或佔位符圖像(如果有)。

3

首先,我強烈建議使用SDWebImage進行異步下載,如果您還沒有這樣做。根據您的需要,您可以將圖像緩存在內存或磁盤上 - 後者通過將它們保存在特殊的緩存文件夾中以正確的方式完成,因此iOS可以在空間不足時將其刪除。它還在UIImageView上提供了一個便利的類別,類似於AFNetworking爲您提供的類別,但具有更多選項和可選的完成塊。這樣,你可以做這樣的事情:

#import <SDWebImage/UIImageView+WebCache.h> 

... 

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

    // Create the cell... 

    [cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"] 
       placeholderImage:[UIImage imageNamed:@"placeholder"] 
        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
     if (image != nil) { 
      [self.images insertObject:image atIndex:indexPath.row]; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 
     } 
    }]; 
    return cell; 
} 

你下載的圖像異步這樣,正確設置的UIImageView,但你也可以使用該塊將圖像保存在一個NSMutableArray,這樣你就可以告訴tableview中的正確的身高。它還會告訴表視圖更新其圖像已加載的單元格的高度。然後,當表視圖需要知道一個給定的小區有多高,如果圖像已經被加載,我們會從中得到大小:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    id image = [self.images objectAtIndex:indexPath.row]; 
    if ([image isKindOfClass:[NSNull class]]) { 
     return defaultHeight; 
    } else { 
     return [image size].height + topPadding + bottomPadding; 
    } 
} 

我們知道是關閉的高度,即使圖像這樣屏幕(例如,當你已經滾動到底部)。此解決方案假定您首先使用NSNull值填充NSMutableArray,然後在加載時用圖像替換它們。最後,如果你願意,甚至可以在顯示單元之前使用SDWebImageManager手動開始下載。

+0

你爲什麼得到這個+50? – Rajneesh071 2013-03-08 07:58:16

+0

我同意SDWebImage是一個偉大的圖書館,但它不應該是這個問題的正確答案,它應該已經得票,但不是賞金點。 – 2013-03-18 14:57:59

3

你可以讓你的UIImageView大小,因爲下載圖像,但它會給壞的樣子

所以我建議你做同樣大小的所有圖像,所以它會顯得精緻而甜美

你可以用它來使相同大小的所有圖像。

+ (UIImage *)imageScaledToSizeWithImage:(UIImage *)imagewww andsizeee:(CGSize)size 
{ 
    //avoid redundant drawing 
    if (CGSizeEqualToSize(imagewww.size, size)) 
    { 
     return imagewww; 
    } 

    //create drawing context 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); 

    //draw 
    [imagewww drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)]; 

    //capture resultant image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //return image 
    return image; 
} 

但如果你真的想顯示不同的行大小表然後進行更改運行時間

你的行大小時,您將獲得的圖像,然後保存圖像與鍵值字典行indexPath 。

[tableImageDict setObject:image forKey:[NSString stringWithFormat:@"%i,%i",indexPath.row,indexPath.section]]; 

然後重新加載您的表格行。

[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
        withRowAnimation:UITableViewRowAnimationNone]; 

它會改變你的行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIImage *image = (UIImage *)[tableImageDict objectForKey: 
           [NSString stringWithFormat:@"%i,%i", 
            indexPath.row,indexPath.section]]; 


    if (image != nil) 
    { 
     return image.size.height; 
    } 
    else{ 
     return 44.0; 
    } 
} 
相關問題