2012-02-19 14 views
27
  1. 這是問題的一個視頻:http://youtu.be/Jid0PO2HgcU細胞ImageView的不出現,直到選擇

  2. 我已經嘗試設置圖像從[電池的ImageView]在一個UITableView時的一個問題資產庫。

圖像被加載,但它不出現在單元格中,直到我觸摸(選擇)該單元格。這裏是我的代碼,設置單元格的ImageView的:

//Start loading the image using the image path 
NSString *path = [occasion imagePath]; 

if (path != nil && [path hasPrefix:@"ass"]) 
{ 
NSLog(@"photo loaded from assets library"); 
//testing ALAssestLibrary 
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; 

ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset) 
{ 
    ALAssetRepresentation *representation = [asset defaultRepresentation]; 
    CGImageRef imageRef = [representation fullResolutionImage]; 
    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef]; 


    [[cell imageView] setImage:[image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)]];  

    [image release]; 
}; 
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){ 

    NSLog(@"FAILED! due to error in domain %@ with error code %d", error.domain, error.code); 
    // This sample will abort since a shipping product MUST do something besides logging a 
    // message. A real app needs to inform the user appropriately. 
    abort(); 
};   

//Convert path to an NSUrl 
NSURL *url = [NSURL URLWithString:path]; 


// Get the asset for the asset URL to create a screen image. 
[assetsLibrary assetForURL:url resultBlock:resultsBlock failureBlock:failureBlock]; 
// Release the assets library now that we are done with it. 
[assetsLibrary release]; 
} 

上面的代碼的一部分:

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

任何想法?

+0

我認爲[這]( http://blog.slaunchaman.com/2011/08/14/cocoa-touch-circumventing-uitableviewcell-redraw-issues-with-multithreading/)文章與您的問題有關。看看它。 – 2012-02-19 19:54:24

回答

39

我認爲在這種情況下,你使用的是iOS定義的UITableViewCell(而不是自定義的),這是你從「initWithStyle:reuseIdentifier:」獲得的單元之一。現在,這些單元格內部構建的方式是,如果不立即分配一些內容,則相應的子視圖將不會添加到單元格的contentView層次結構中。這種行爲是典型的複雜自定義視圖,其中最終的單元格佈局只有在所有子視圖的內容完全指定時才能確定。 (例如,您有兩個標籤,假設標籤A和標籤B,標籤A是多行,並且您希望在標籤-A的正下方添加標籤-B,則只有標籤具有標籤A,才能正確放置標籤B.並計算其高度)。所以我猜想內置的iOS表格單元格是以相同的方式完成的,子視圖層次結構是在「layoutSubviews」階段構建的。

在你的情況下,你創建單元格,但推遲提供圖像的時間。但是在這一點上,layoutSubviews已經被調用,沒有任何圖像,相應的imageView甚至沒有被分配並添加到contentView層次結構中!

所以,根據我的,你的解決方案就是立即指派爲您的資產是「佔位符」(佔位當然可以透明圖像),這將保證你內部的UIImageView創建的。出於上面解釋的相同原因,小心圖像大小,它應該接近預期的圖像大小。一旦完成塊被調用,圖像視圖應該已經存在,圖像就會出現。

當你點擊單元格時出現圖像的原因是由於此操作再次調用layoutSubviews。在這種情況下,可能會重新執行整個代碼,並且在設置內部「image」屬性之前已經調用了「setImage:」,並且contentView層次結構將與新圖像一起重建。

你的問題的另一個可能的解決方案當然是使用「自定義」UITableViewCell(例如從一個筆尖加載)。在這種情況下,你所有的子視圖都會被加載,你只需使用他的標籤訪問UIImageView(閱讀蘋果文檔以瞭解更多關於這種有用的技術來從Nib文件創建表視圖單元格)。

+0

謝謝。非常豐富。 – Ali 2012-02-20 12:37:04

+1

我做了佔位符解決方案,它的工作原理像魔術一樣。我被困了一段時間。這裏是if語句之後的代碼:'UIImage * placeholderImage = [UIImage imageNamed:@「placeholder.png」]; [[image imageView] setImage:[placeholderImage imageByScalingAndCroppingForSize:CGSizeMake(36.0,42.0)]];' – Ali 2012-02-20 13:02:06

+2

Viggio在對問題的評估中是正確的,但更簡單的解決方法是調用layoutSubviews方法在將圖像分配給單元格圖像視圖後,立即將該單元格放入圖像中這將導致單元格再次通過佈局過程。由於這個圖像現在出現,它會出現! – mklbtz 2015-03-24 19:52:36

1

這是我的路我是如何解決這些問題,我知道是不是最好的,但該工作:)

UIImage *thumbnailImage = ... 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [cell.imageView setImage:thumbnailImage]; 

     UITableViewCellSelectionStyle selectionStyle = cell.selectionStyle; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [cell setSelected:YES]; 
     [cell setSelected:NO]; 
     cell.selectionStyle = selectionStyle; 
    }); 
+0

謝謝!工作完美 – 2014-06-08 21:46:04

+5

請不要這樣做! – 2015-07-09 10:18:09

6

我抓傷我的頭了這麼久,終於想通了。

我的錯誤是我在cell.imageView中設置圖像時,我應該設置我的實際出口cell.eventImageView。它與UITableViewCell中提供的通用imageview混淆。希望它有助於某人。

25

需要佈局單元格圖像設置

後就像

[細胞setNeedsLayout]

+0

謝謝,它解決了問題 – 2015-12-05 20:11:30

+0

謝謝!解決了我的問題。 – shadowmoses 2016-04-01 02:03:49

1

在我來說,我是通過一個故事板建立一個原型細胞,但是卻意外地使用dequeueCellWithIdentifier:forIndexPath方法,而不是dequeueCellWithIdentifier:

第一種方法僅在通過編程式UITableView registerClass:forReuseIdentifier:registerNib:forReuseIdentifier方法將單元格佔用到tableview時纔會使用。

0

我有類似的問題。我已經註冊了默認的UITableViewCell類而不是我的自定義類。

我有這樣的:

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: visibilityCellIdentifier) 

,但我應該有這樣的:

tableView.registerClass(VisibilityCell.self, forCellReuseIdentifier: visibilityCellIdentifier) 

細胞創作都看着要工作,我通過調試加強,但沒有露面,直到我選中每一行。

0

有時它發生在使用自定義UITableViewCell與標籤,圖像等,但也有一些地方,你正在設置單元格的默認UILabel。例如

customCell.textLabel.text = @"some text" 

要解決此問題,請避免使用默認標籤,而是將自己的UILabel添加到您的自定義單元格中。

0

我得到同樣的問題,當我使用此代碼:

cell = [tableView dequeueReusableCellWithIdentifier:kCellSection1 forIndexPath:indexPath]; 
     if (indexPath.row == 0) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1]; 
      cell.textLabel.text = @"Vote Up for me if it help for you!"; 
     } 

但我通過修復更換:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1]; 

要:

cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1];