2013-03-12 61 views
0

這是我已經試過到目前爲止動態改變細胞和UIImage的基於實際圖像的比率高度

我定義一個全局變量

float imgHeight; 

然後在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我這樣做

 size_t pixelsWide = CGImageGetWidth(scribbleImage.image.CGImage); 
     size_t pixelsHigh = CGImageGetHeight(scribbleImage.image.CGImage); 
     CGFloat ratio = 312/pixelsWide; 
     pixelsHigh = pixelsHigh*ratio; 

然後在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

return pixlesHeight; 

現在因爲heightForRowAtIndexPath被稱爲cellForRowAtIndexPath之前,我總是得到pixelsHeight as 0

我想不出另一種方式做到這一點。我是否應該在heightForRow中重新定義UIImage並執行此操作?

[scribbleImage setImageWithURL: [NSURL URLWithString:scribble[@"scribble_image"]] placeholderImage:[UIImage imageNamed:@"Default.png"]]; 

那裏還有嗎?應該有一個更簡單的方法來做到這一點。

+0

您的桌子一次只有一張圖片嗎?它在每一行嗎? – rdelmar 2013-03-12 23:51:09

+0

這是非常行。每一行在單元格中只有一個圖像 – Jonathan 2013-03-12 23:56:39

+0

相同的圖像或不同的圖像? – rdelmar 2013-03-12 23:58:24

回答

0

在heightForRowAtIndexPath中執行該操作。正如我理解您對浪費處理和加載時間的擔憂一樣,只需將UIImage存儲在您的項目數組中,以便在cellForRowAtIndexPath中使用同一個項目,從而避免創建新項目。