簡體   English   中英

UICollectionViewCell:圖像和大小更改后,不刷新帶有UIImageView項目的已重用單元格

[英]UICollectionViewCell : Reused Cell with UIImageView item not refreshed after image and size changes

我已經開始使用UICollectionView開發具有pinterest式布局的應用程序,並具有不同的單元格高度和內容。 我定義了一個模板單元格(標簽+ UIImageView),該模板單元格通過cellForItemAtIndexPath回調使用。

在該特定函數(cellForItemAtIndexPath)中,我正在修改內容(UIImage)和UIImageView元素的大小,以尊重新圖像的比例並使用當前Cell的完整寬度。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

FRGWaterfallCollectionViewCell *waterfallCell = [collectionView dequeueReusableCellWithReuseIdentifier:WaterfallCellIdentifier forIndexPath:indexPath];

[waterfallCell.layer setMasksToBounds:YES];
[waterfallCell.layer setCornerRadius:10.0];
[waterfallCell.layer setBorderWidth:1.0];
[waterfallCell.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
[waterfallCell.layer setNeedsDisplayOnBoundsChange:YES];

// some variables init
.....

// Get the new image
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat: @"photo_%d.jpg", no_image]];

// Compute the image ratio
CGFloat largeur_img = image.size.width;
CGFloat hauteur_img = image.size.height;
CGFloat viewRatio = hauteur_img / largeur_img;

// Modify the UIImageView frame
CGRect cadre = waterfallCell.img_doc.frame;
<b>waterfallCell.img_doc.frame = CGRectMake(cadre.origin.x,cadre.origin.y,cadre.size.width, ceil(cadre.size.width * viewRatio));</b>

// We set the image of the UIImageView
<b>[waterfallCell.img_doc setImage:image];</b>

// Force to redraw the cell
<b>[waterfallCell setNeedsDisplay];</b>

return waterfallCell;

}

但是,此單元格不會刷新或重繪...除非我向下滾動UICollectionView並返回頂部,然后每個單元格的UIImageView形狀正確。 我在網上搜索過,看到了很多有關setNeedsDisplay的帖子,但是在這里似乎Cell使用了CALayer,所以它並不在意。.我了解主線程上的刷新,但是我不知道如何觸發為了獲得第一個顯示,此刷新將顯示具有正確sub-UIImageView以及正確尺寸的正確單元格。

謝謝你的幫助,

問候

尼古拉斯

問題就在這里:

FRGWaterfallCollectionViewCell *waterfallCell = [collectionView dequeueReusableCellWithReuseIdentifier:WaterfallCellIdentifier forIndexPath:indexPath];

第一次打開控制器時,沒有要出隊的單元格,所以WaterfallCell為零。 (如果需要,您可以添加日志進行驗證)。 只有當您滾動時,單元格才會出隊,這就是為什么只有當您滾動時才能看到差異。

使用自定義集合視圖單元格時,我該怎么做:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"catalogOneRowCell";
    LACatalogOneRowCell *catalogCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    if (catalogCell == nil) {
        catalogCell = [LACatalogOneRowCell getLACatalogOneRowCell];
    }

這是LaCatalogOneRowCell.m

 + (LACatalogOneRowCell *)getLACatalogOneRowCell {
      NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"LACatalogOneRowCell" owner:nil options:nil];
      for (NSObject *obj in xib) {
          if ([obj isKindOfClass:[LACatalogOneRowCell class]]) {
              return (LACatalogOneRowCell *)obj;
          }
      }
     return nil;

}

非常重要的是,在viewDidLoad中,注冊單元的筆尖:

UINib *nib = [UINib nibWithNibName:@"LACatalogOneRowCell" bundle:nil];
[catalogCollectionView registerNib:nib forCellWithReuseIdentifier:@"catalogOneRowCell"];

另外,我不建議在cellforRow中定位:

UIImage *image = [UIImage imageNamed:... 

與UIImage * image = [[[UIImage alloc] initWithImage:....] autorelease]相同;

在cellForRow中分配對象對性能不利,尤其是像UIImage這樣的對象

我認為,您應該基於比率出隊。 這將使您免於嘗試將所有內容放在一起的痛苦。 無論發生什么情況,您的物品都會有一組有限的比率。 另外,我不確定您是否可以更改已經使用的單元格的框架而無需重新啟動視圖的大量工作,這在CPU方面將是非常昂貴的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM