objective-c - 由于文本标签而导致 UICollectionView 滚动不稳定

标签 objective-c performance uicollectionview uicollectionviewcell

我遇到了 UICollectionView 滚动不稳定的问题。我的单元格由图片和一些文本标签组成。

我尝试检查导致此问题的原因,当我禁用为标签设置文本时,一切都很顺利,当我为两个以上标签设置文本时,滚动再次变得不稳定......

有谁知道怎么解决吗?

这是我的代码:

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

    static NSString *CellIdentifier = @"ResultCell";
    ResultCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    if ([self.artworksSavedForLater containsObject:indexPath]) {
        cell.saveForLaterButton.selected = YES;
    } else cell.saveForLaterButton.selected = NO;
    Artwork *artwork = [self.dataHelper.fetchedResultsController objectAtIndexPath:indexPath];
    cell.title.text = artwork.title;


    cell.owner.text = artwork.owner;
    cell.fields.text = artwork.fields;
    cell.numberOfViews.text = [NSString stringWithFormat:@"%@",artwork.views];
    cell.numberOfLikes.text = [NSString stringWithFormat:@"%@",artwork.likes];
    cell.numberOfComments.text = [NSString stringWithFormat:@"%@",artwork.comments];
    cell.publishedDate.text = artwork.publishedDate;
    if ([artwork.hasThumbnail boolValue]) {
        dispatch_queue_t downloadQueue = dispatch_queue_create("imagecache", NULL);
        dispatch_async(downloadQueue, ^{
            UIImage *productImage = [UIImage imageWithData:artwork.thumbnail];
            CGSize imageSize = productImage.size;
            UIGraphicsBeginImageContext(imageSize);
            [productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
            productImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            dispatch_async(dispatch_get_main_queue(), ^ {
                cell.thumbnailImage.image = productImage;
                cell.thumbnailImage.hidden = NO;
            });
        });


    }
    else {
        cell.thumbnailImage.hidden = YES;
        [self startOperationsForPhotoRecord:artwork atIndexPath:indexPath];

    }




    return cell;

我的手机代码:

- (void)didMoveToSuperview {
    [super didMoveToSuperview];



    self.title.font = [UIFont fontWithName:@"VerbRegular" size:14.0];

    self.layer.borderColor = [UIColor colorWithWhite:0.19f alpha:1.0f].CGColor;
    self.layer.borderWidth = 1.0f;

    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;



}

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.thumbnailImage.image = nil;
}

最佳答案

我找到了动态设置许多标签的 UICollectionView 滚动不稳定的解决方案。

我不使用标签,而是使用 Miroslav Hudak 在这篇文章中描述的 drawLabel 方法绘制所有 NSString:

How to change color of NSString in drawAtPoint

现在我的 UICollectionView 可以平滑滚动:)

关于objective-c - 由于文本标签而导致 UICollectionView 滚动不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885895/

相关文章:

iOS 8 - UIBarButtonItem 推送 Segue 覆盖操作

objective-c - 在 map View 上绘制 MKCircle

iphone - NSPredicate 与 NSString : Which is better/faster for finding superstrings?

c# - 各种列表初始化和填充技术的性能差异

java - Spark 2.2.0 : How to remove Specific Duplicates from a Dataset of a list column

ios - 获取 UICollectionView 的按下和常量坐标

ios - 如何在我的 UICollectionView 单元格中设置标签?

iphone - 获取 NSFileWrapper 的路径

javascript - 为什么在jquery中缓存ID时使用索引0?

ios - 重新加载 UICollectionView 部分并保留滚动位置