将代码从cellForItemAtIndexPath传输到CollectionViewCell(Parse Back-End)

时间:2015-08-04 19:07:57

标签: ios objective-c parse-platform uicollectionview

我正在使用Parse作为我的应用程序的数据库。我想创建一个CollectionViewCell并在那里传输我的代码,而不是将它放在View Controller的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;
}

Cell.h

@interface ProductCell : UICollectionViewCell

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

@end

2 个答案:

答案 0 :(得分:0)

请记住,当单元格滚动到视图中时,会一遍又一遍地调用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中,我们不能指望在图像到达时集合的状态是相同的,所以不是保留在完成块中操作单元格,只需重新加载索引路径...... / p>

- (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;
}

答案 1 :(得分:0)

在自定义单元格中创建一个暴露在.h文件中的方法。

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

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

在该方法的实现中,从对象中提取细节并将它们分配给各自的属性。