0

我有一个测试,我想加载一个 UICollectionViewCell ( CustomCollectionViewCell),将一些数据传递给它,并检查单元格的标签是否用这些数据更新。

cell.nameLabel是单元格的 UILabel 并且该方法setText被调用但文本本身永远不会更新。

cell.nameLabel.text始终返回 xib 中定义的初始文本。

标签定义如下:

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;

和规格:

SPEC_BEGIN(CustomCollectionViewCellSpec)

describe(@"CustomCollectionViewCell", ^{

    __block CustomCollectionViewCell *cell = nil;
    __block CustomCollectionViewCellData *cellData = nil;

    beforeEach(^{

        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomCollectionViewCell"
                                                         owner:self
                                                       options:nil];

        for (id object in objects) {

            if ([object isKindOfClass:[CustomCollectionViewCell class]]) {

                cell = (CustomCollectionViewCell *)object;

                break;
            }
        }

        cellData = [[CustomCollectionViewCellData alloc] init];
        cellData.name = @"Custom name";
    });

    afterEach(^{

        cell = nil;
        cellData = nil;
    });

    it(@"should populate the views with the cell data", ^{

        [[cell.nameLabel should] receive:@selector(setText:)
                           withArguments:cellData.name];

        [cell configureWithCellData:cellData];

        [[cell.cellData should] equal:cellData];

        [[cell.nameLabel.text should] equal:cellData.name];
    });
});

SPEC_END
4

1 回答 1

0

问题是您如何创建单元格。您不能分配初始化单元格或获取笔尖。你要一个收藏品。

对于您需要的收藏:

    UICollectionView *collection = [[UICollectionView alloc] init];
    UICollectionCell *cell = [collection dequeueReusableCellWithReuseIdentifier:@"yourCellIdentifier" forIndexPath:0];

在此之后,您将能够获取您的单元格并对其进行修改,而无需将其所有属性都设置为 nil 属性。

于 2014-11-17T11:36:54.667 回答