【问题标题】:Caching UITableViewCells缓存 UITableViewCells
【发布时间】:2014-10-20 00:24:23
【问题描述】:

我有一个带有UITableView 的视图,它可以显示4 种类型的UITableViewCell(自定义)。基本上,其中两个带有媒体(图像、移动、声音),其中两个带有额外的用户头像。

让我们概括一下,带有媒体的单元格将仅显示图像(缩略图)。当然,这些图像是在创建单元格时动态下载的。每次用户滚动表格视图时下载这些图像可能是有害的,所以我使用EGOCache 来缓存这些图像但是...... 这对解决滚动问题没有帮助!我认为缓存会将 pionter 存储到这些图像,但每次重新创建单元格时,它都会从磁盘获取该图像(因此仪器告诉我这种方法正在扼杀我的性能)。

我的问题是:如何缓存UITableViewCell,这样就不会在我每次滚动UITableView 时创建它?


以下是我的代码示例,您可以想象我的问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Event *event = [self.events objectAtIndex:indexPath.row];

    if ([event.type isEqualToString:@"camp"]) {
        if (!event.mediaType) {
            CampCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[CampCell identifier]];
            [cell configureWithModel:event];
            return cell;
        } else {
            CampMediaCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[CampMediaCell identifier]];
            [cell configureWithModel:event];
            return cell;
        }
    } else {
        if (!event.mediaType) {
            StatusCell * cell = [self.tableView dequeueReusableCellWithIdentifier:[StatusCell identifier]];
            [cell configureWithModel:event];
            return cell;
        } else {
            StatusMediaCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[StatusMediaCell identifier]];
            [cell configureWithModel:event];
            return cell;
        }
    }
}

这就是configureWithModel: 的样子:

- (void)configureWithModel:(id)model {
    if ([model isKindOfClass:[Event class]]) {
        Event *event = model;
        self.titleLabel.text = event.title;
        self.locationLabel.text = event.address;
        self.timeLabel.text = [self hoursLeftToDate:event.expirationDate];

        UIImageView *imageAttachedToStatus = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.mediaContainerView.frame.size.width, self.mediaContainerView.frame.size.height)];
        imageAttachedToStatus.contentMode = UIViewContentModeScaleAspectFill;
        imageAttachedToStatus.clipsToBounds = YES;

        [self getMediaAttachmentForModel:model completion:^(id attachment) {
            if (attachment) {
                imageAttachedToStatus.image = (UIImage *)attachment;
            }
        }];

        [self.mediaContainerView addSubview:imageAttachedToStatus];
    }
}

当然,您可能想知道getMediaAttachmentForModel:completion: 的样子...

- (void)getMediaAttachmentForModel:(Event *)model completion:(void (^)(id attachment))completion {
    NSString *mediaID = [NSString stringWithFormat:@"media%li", (long)model.eventID];

    if ([[EGOCache globalCache] hasCacheForKey:mediaID]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            id cachedAttachment =[[EGOCache globalCache] objectForKey:mediaID];
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(cachedAttachment);
            });
        });
    } else {
        [[Client sharedClient] fetchMediaThumbnailForEvent:model completion:^(id attachment, NSError *error) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [[EGOCache globalCache] setObject:attachment forKey:mediaID];

                dispatch_async(dispatch_get_main_queue(), ^{
                    completion(attachment);
                });
            });
        }];
    }
}

【问题讨论】:

    标签: ios objective-c uitableview caching


    【解决方案1】:

    正确的解决方案是找到/实现一个能够将图像缓存在内存中的缓存,而无需每次都从磁盘中获取它们。

    看看Path的FastImageCache

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 2012-03-10
      • 2013-05-20
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多