将代码从cellForItemAtIndexPath传输到CollectionViewCell(解析后端) [英] Transfering code from cellForItemAtIndexPath to a CollectionViewCell (Parse Back-End)

查看:72
本文介绍了将代码从cellForItemAtIndexPath传输到CollectionViewCell(解析后端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Parse as the database for my app. I want to create a CollectionViewCell and transfer my code there, instead of having it inside the View Controller's cellForItemAtIndexPath. How do I do this?

谢谢.

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

推荐答案

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

create a method in your custom cell which is exposed in your .h file.

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

This method should receive an argument of type PFObject.

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

Then in you cellForItemAtIndexPath, call that method and pass your object in that method.

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

And in the implementation of that method, extract the details from your object and assign them to respective properties.

这篇关于将代码从cellForItemAtIndexPath传输到CollectionViewCell(解析后端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆