繁体   English   中英

将代码从cellForItemAtIndexPath传输到CollectionViewCell(解析后端)

[英]Transfering code from cellForItemAtIndexPath to a CollectionViewCell (Parse Back-End)

我正在使用Parse作为我的应用程序的数据库。 我想创建一个CollectionViewCell并将代码传输到那里,而不是将其放入视图控制器的cellForItemAtIndexPath中。 我该怎么做呢?

谢谢。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"productCell";

    ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    PFObject *product = [self.products objectAtIndex:indexPath.row];

    NSString *price = [NSString stringWithFormat:@"$%@.00", product[@"price"]];

    cell.price.text = price;

    PFFile *userImageFile = product[@"firstThumbnailFile"];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            UIImage *thumbnailImage = [UIImage imageWithData:imageData];
            UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

            cell.image.image = thumbnailImageView.image;
        }
    }];

    return cell;
}

单元格

@interface ProductCell : UICollectionViewCell

@property (nonatomic, weak) IBOutlet UIImageView *image;
@property (nonatomic, weak) IBOutlet UILabel *price;

@end

请记住,随着单元格滚动到视图中, cellForIndexPath调用cellForIndexPath 因此,以这种方法发出不受保护的网络请求是一种不好的做法。

如果要懒惰地获取图像,请添加逻辑来缓存检索到的结果,并且仅获取以前从未获取过的图像...

// in private interface
@property(strong,nonatomic) NSMutableDictionary *imageForProduct;

// in init
self.imageForProduct = [@{} mutableCopy];

一种获取图像的方法...

- (void)imageForProduct:(PFObject *)product completion:(void (^)(UIImage *))completion {
    PFFile *userImageFile = product[@"firstThumbnailFile"];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        UIImage *image;
        if (!error) {
            image = [UIImage imageWithData:imageData];
        }
        completion(image);
    }];
}

现在,在cellForIndexPath中,我们不能指望图像到达时集合的状态是相同的,因此,与其保留在完成块中操纵单元,不如重新加载索引路径...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"productCell";
    ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    PFObject *product = [self.products objectAtIndex:indexPath.row];
    NSString *price = [NSString stringWithFormat:@"$%@.00", product[@"price"]];
    cell.price.text = price;

    if (self.imageForProduct[product.objectId]) {
        cell.image = self.imageForProduct[product.objectId];
    } else {
        cell.image = // optionally put a placeholder image here
        [self imageForProduct:product completion:^(UIImage *)image {
            self.imageForProduct[product.objectId] = image;
            [collectionView reloadItemsAtIndexPaths:@[indexPath]];
        }];
    }
    return cell;
}

在您的.h文件中公开的自定义单元中创建一个方法。

此方法应接收类型为PFObject的参数。

然后在您的cellForItemAtIndexPath中,调用该方法并在该方法中传递您的对象。

在该方法的实现中,从对象中提取详细信息,并将其分配给相应的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM