【问题标题】:Custom UICollectionViewCell properties are null after dequeueing出列后自定义 UICollectionViewCell 属性为空
【发布时间】:2015-12-23 17:49:45
【问题描述】:

所以我有一个 UICollectionView 和一个自定义单元格,它们都显示出来并且效果很好。我在 viewDidLoad 中注册了这个类:

myCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
[myCollectionView setDataSource:self];
[myCollectionView setDelegate:self];
[myCollectionView setBackgroundColor:[UIColor myColor]];
[myCollectionView registerClass:[SBCustomCell class] forCellWithReuseIdentifier:@"Cell"];

在我的 cellForItemAtIndexPath 方法中,我将单元格出列并设置其属性并返回它:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";

SBCustomCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

[cell setName: "My Name"];
cell.image = [UIImage imageNamed:@"lol.png"];
cell.score = 100.0;
cell.backgroundColor=[UIColor whiteColor];

return cell;
}

这一切都很好,它会显示在 UI 中。我的问题是当我在 collectionView 上设置手势识别器时,当我长按某个单元格时,我希望能够访问它的属性。我正在尝试这样做:

-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {

CGPoint locationPoint = [longPressRecognizer locationInView:myCollectionView];

if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {

    NSIndexPath *indexPathOfMovingCell = [myCollectionView indexPathForItemAtPoint:locationPoint];

    SBCustomCell *cell= [myCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPathOfMovingCell];

    NSLog(@"%@",cell.name);

当我尝试访问我的任何自定义单元格的属性时,它在控制台中为 (null)。这是为什么?我做错了什么?

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    你需要使用:

    SBCustomCell* cell = (SBCustomCell*)[myCollectionView cellForItemAtIndexPath:indexPathOfMovingCell];
    

    代替:

    SBCustomCell* cell = [myCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPathOfMovingCell];
    

    另外,您使用SBSingleCandidateUnitView 作为单元格类型,而不是您的SBCustomCell

    【讨论】:

    • 嗨,迈克,感谢您的回复。实际上,我确实将它作为 SBCustomCell 使用,我只是在编辑用于 StackOverflow 的代码而忘记更改该代码。我的问题是为什么我不使用出队方法?我想取回自定义单元格而不是通用 UICollectionView 单元格。通用单元格没有我设置的自定义属性。
    • @stellarowl12:您不使用出队,因为这会为您提供一个“新”单元格以添加到集合视图中。即使该方法返回 UICollectionViewCell,您也可以将其转换为实际的单元格类型。我已经更新了我的答案以显示这一点。
    • 所以我投了它并且没有错误或警告,但是当我尝试使用 NSLog 打印出我在 cellForItemAtIndexPath 中设置的任何自定义单元格的属性时,它们仍然打印为 Null。 ...知道为什么会这样吗?我是不是放错地方了?
    • @stellarowl12:对不起,我想我不能真正给你任何准确的想法,为什么它会这样做。我认为,调试是你最好的选择。
    • 所以只是为了澄清。我可以在 cellforitematindexpath 中使用出队的东西,但不能在 handleLongPress 方法中使用,对吧?
    【解决方案2】:

    这是因为我没有正确设置我的属性。在我的头文件中,我需要设置一个属性,也就是 @property ... UIImageView myImageView;

    在我的 CustomCell.m 文件中,我不应该覆盖那些 setter 和 getter。相反,只需分配并初始化它们并将它们添加到视图中。

    回到我的 ViewController.m 我应该添加这样的属性:

    customcell.myImageView.image = [UIImage imageNamed:@"cartman.png"];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多