![](/img/trans.png)
[英]Cell in UITableView doesn't update until selected in iOS (swift)
[英]cell imageView in UITableView doesn't appear until selected
這是一個問題的視頻:http: //youtu.be/Jid0PO2HgcU
嘗試從資產庫中為UITableView中的[cell imageView]設置圖像時出現問題。
圖像已加載但在觸摸(選擇)該單元格之前不會出現在單元格中。 這是我的代碼設置單元格的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
有任何想法嗎?
我想在這種情況下你使用iOS定義的UITableViewCell(而不是自定義的),這是你從“initWithStyle:reuseIdentifier:”得到的那些單元格之一。 現在這些單元格內部構建的方式是,如果您不立即分配某些內容,則不會在單元格的contentView層次結構中添加相應的子視圖。 這種行為是復雜自定義視圖的典型行為,其中最終單元格布局只有在其所有子視圖的內容都已完全指定時才能確定。 (例如,你有兩個標簽,比如標簽A和標簽B,標簽A是多行的,你想在標簽A的正下方添加標簽B,那么只有在你有標簽-A內容后才能正確放置標簽-B並計算它的高度。 所以我認為內置的iOS表格單元格以相同的方式完成,子視圖層次結構是在“layoutSubviews”階段構建的。
在您的情況下,您創建單元格,但推遲提供圖像的時間。 但此時layoutSubviews已經被調用,沒有任何圖像,相應的imageView甚至沒有被分配並添加到contentView層次結構中!
因此,根據我的說法,您的解決方案就是立即為您的資產分配一個“占位符”(占位符當然可以是透明圖像),這將保證您創建內部UIImageView。 小心圖像大小,它應該接近預期的圖像大小,原因與上面解釋的相同。 調用完成塊后,圖像視圖應該已經存在,圖像將會出現。
當您點擊圖像出現的單元格時,原因是此操作再次調用layoutSubviews。 在這種情況下,可能會重新執行整個代碼,並且在此之前已經調用了“setImage:”,然后設置內部“image”屬性,並使用新圖像重建contentView層次結構。
您問題的另一個可能的解決方案當然是使用“自定義”UITableViewCell(例如從Nib加載)。 在這種情況下,所有子視圖都將被加載,您只需使用他的標簽訪問UIImageView(閱讀apple文檔以了解更多有關從Nib文件創建表視圖單元的有用技術)。
你需要在圖像集后布局單元格
就像
[cell setNeedsLayout];
我摸不着頭腦,終於想通了。
我的錯誤是我在設置我的實際插座cell.eventImageView
時在cell.imageView
中設置圖像。 它搞亂了UITableViewCell
提供的通用imageview。 希望它對某人有幫助。
這是我如何解決這些問題的方式,我知道這不是最好但是工作:)
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;
});
在我的例子中,我是通過Storyboard設置原型單元格,但是偶然使用了dequeueCellWithIdentifier:forIndexPath
方法而不是dequeueCellWithIdentifier:
.
第一種方法僅在通過程序化UITableView registerClass:forReuseIdentifier:
或registerNib:forReuseIdentifier
方法將單元格分配到tableview時使用。
我遇到了類似的問題。 我已經注冊了默認的UITableViewCell類而不是我的自定義類。
我有這個:
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: visibilityCellIdentifier)
但我應該有這個:
tableView.registerClass(VisibilityCell.self, forCellReuseIdentifier: visibilityCellIdentifier)
當我逐步調試調試器時,單元格創建看起來都有效,但在我選擇每一行之前沒有任何內容出現。
有時,當您使用帶有標簽,圖像等的自定義UITableViewCell
時,您也會在某處設置默認的單元格UILabel。 例如
customCell.textLabel.text = @"some text"
要解決此問題,請避免使用默認標簽,而是在自定義單元格中添加自己的UILabel。
我使用此代碼時遇到同樣的問題:
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.