ios - 自定义 CollectionViewCell 属性为零

标签 ios objective-c uicollectionview uicollectionviewcell

我正在使用自定义 CollectionView Cell 实现自定义 Collection View 。

这是我对 CollectionView 的实现。

- (void)setUpCollectionView{
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];

    [self.vwCarCollection setDataSource:self];
    [self.vwCarCollection setDelegate:self];

    [self.vwCarCollection registerClass:[FindMyCarCell class] forCellWithReuseIdentifier:@"FindMyCarCell"];
    [self.vwCarCollection setBackgroundColor:[UIColor redColor]];

    [self.vwCarCollection reloadData];
    [self.vwCarCollection reloadInputViews];
}

我在“ViewDidLoad”上调用了这个函数。

这是我对 UICollectionViewDataSource 的实现

- (UICollectionViewCell  *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FindMyCarCell *cell = (FindMyCarCell *)[self.vwCarCollection dequeueReusableCellWithReuseIdentifier:@"FindMyCarCell" forIndexPath:indexPath];
    cell.vwImgMain.image = [UIImage imageNamed:@"sample_car"];
    cell.lblCarNo.text = @"GH2334343";
    return cell;
}

当我调试我的代码时,单元格实例是这样的

<FindMyCarCell: 0x78645b30; baseClass = UICollectionViewCell; frame = (302 50; 221 180); layer = <CALayer: 0x786a07a0>>

但是当我尝试更新自定义单元格的 ImageView 时,它只是返回我的 ImageView 为零。 我的实现有什么问题。谁能建议我?谢谢。

最佳答案

将 ImageView 添加到单元格时可能会出现一些错误。如果您在 Storyboard 中创建了您的单元格,那么请不要在 viewDidLoad 中注册您的单元格类。而是在 Storyboard 中设置重用标识符,否则使用自定义 Collection View 单元类的 frame 方法将 ImageView 添加到 init 中。

关于ios - 自定义 CollectionViewCell 属性为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43605155/

相关文章:

objective-c - 从 NSCollectionView 拖到 NSOutlineView

ios - UIView 对象可以充当 UITableView 对象吗?

ios - 如何根据 numberOfItemsInSection 的数量设置 heightForRowAt?

swift - UITextfield 在 UICollectionView reloadData() 之后成为FirstResponder

ios - 尝试使用插图隐藏 UITableViewCell 分隔符,但它只会降低不透明度

objective-c - NSString stringByReplacingOccurrencesOfRegex

ios - SpriteKit linearDamping - 最大?

ios - 具有 BOSH 支持的 ios 的 XMPP 客户端库

ios - UICollectionView 窃取焦点

ios - UINavigationView不能弹出,只有导航弹出动画,UIViewController没变