以编程方式创建自定义UICollectionViewCell并设置属性

时间:2015-07-02 21:19:02

标签: ios objective-c uicollectionview uicollectionviewcell

我一直在尝试配置一个自定义UICollectionViewCell,它应该呈现一个UIView,其中包含一些使用自定义方法生成的子视图。

问题在于,当我尝试将所需数据从-cellForItemAtIndexPath传递到单元格属性(NSStrings)时,应用程序崩溃并且数据似乎根本没有传递,因为NSLog返回(null)表示所有属性。

我认为问题在于未初始化属性,但是当我在-initWithFrame中初始化它们时,它们并没有返回任何值。我试图使用-initWithCoder和-awakeFromNib但它没有用。

我还尝试使用自定义方法为我设置属性。虽然它似乎有效,但我似乎找不到将生成的视图添加到单元格视图的方法,因为使用[self.contentView addSubview:...]似乎是在添加每个生成的查看集合视图中的每个单元格。

如果你们能帮助我,我会非常感激 - 我整天都花在这上面,而我却无法让它发挥作用。也许我应该以不同的方式做到这一点?

我的自定义收藏视图单元的.h文件:

#import "SHDesignChicago.h"

@interface SHDesignChicagoCollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *date;
@property (strong, nonatomic) NSString *going;

@property (strong, nonatomic) PFFile *background;
@property (strong, nonatomic) UIView *generatedDesign;

- (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background;

@end

我的自定义集合查看单元格.m文件:

#import "SHDesignChicagoCollectionViewCell.h"

@implementation SHDesignChicagoCollectionViewCell

- (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background {

    UIImage *backgroundImage = [[UIImage alloc] initWithData:[background getData]];

    NSLog(@"Event details:\nName: %@\nDate: %@\nGoing: %@\nBackground: %@", name, date, going, background);

    self.generatedDesign = [SHDesignChicago designWithName:name date:date going:going background:backgroundImage frame:self.frame];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        //self.autoresizesSubviews = YES;

        __block UIImage *backgroundImage;
        [self.background getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error) backgroundImage = [[UIImage alloc] initWithData:data];
        }];

        NSLog(@"Event details:\nName: %@\nDate: %@\nGoing: %@\nBackground: %@", self.name, self.date, self.going, self.background);
        if (self.name != nil) {
            UIView *generatedDesign = [SHDesignChicago designWithName:self.name date:self.date going:self.going background:backgroundImage frame:self.frame];
            [self addSubview:generatedDesign];
        }

    }
    return self;
}
@end

还有cellForItemAtIndexPath:

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

    PFObject *event = [self.events objectAtIndex:indexPath.row];

    NSString *name = event[@"eventName"];
    NSDate *date = event[@"eventDate"];
    PFFile *background = event[@"coverPhoto"];

    if ([event[@"designId"] isEqual: @(1)]) {
        // SHDesignChicago
        SHDesignChicagoCollectionViewCell *chicagoCell = (SHDesignChicagoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"chicago" forIndexPath:indexPath];
        /*
        chicagoCell.name = @"TEST";
        chicagoCell.date = @"TODAY, 5 PM";
        chicagoCell.going = @"NATALIA KAWECKA AND 35 OTHERS ARE GOING";
        chicagoCell.background = background;
        */

        [chicagoCell setCellWithEventName:name eventDate:@"TODAY, 5 PM" eventGoing:@"NATALIA M AND 35 OTHERS ARE GOING" eventBackground:background];

        return chicagoCell;
    }

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    return cell;
}

0 个答案:

没有答案