子类化 UICollectionViewCell 导致永远不会被选中 [英] Subclassing UICollectionViewCell leads to never being selected

查看:23
本文介绍了子类化 UICollectionViewCell 导致永远不会被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 UICollectionViewCell 子类化并从 nib 文件加载:

I've tried subclassing a UICollectionViewCell and loading from a nib file:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"DatasetCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];

        UIView *view = [UIView new];
        view.frame = self.frame;
        view.backgroundColor = [UIColor orangeColor];
        self.selectedBackgroundView = view;
    }

    return self;
}

我遇到了选择单元格的问题,cell.selected 没有被设置.总是 NO 导致取消选择单元格的问题.

I'm running into an issue where a cell is selected, the cell.selected is not being set. It is always NO which is leading to an issue of deselecting the cells.

如何让单元格进入选中状态?

How do I handle getting the cell to the selected state?

我最初将自定义 UICollectionViewCell 作为类加载:

I originally loading the custom UICollectionViewCell as a class:

[collectionView registerClass:[DatasetCell class] forCellWithReuseIdentifier:@"dataCell"];

切换到加载笔尖:

[collectionView registerNib:[UINib nibWithNibName:@"DatasetCell" bundle:nil] forCellWithReuseIdentifier:@"nibCell"];

我在两个方面都有相同的选择/取消选择问题.

I have the same select/deselect issue both ways.

推荐答案

主要错误是你定义了一个属性

The main error is that you have defined a property

@property (nonatomic) BOOL isSelected;

在您的自定义 UICollectionViewCell 子类中(在DatasetCell.h"中),这会干扰 UICollectionViewCell 继承的selected"属性.

in your custom UICollectionViewCell subclass (in "DatasetCell.h"), which interferes with the inherited "selected" property of UICollectionViewCell.

如果您删除该属性定义,选择和取消选择将按预期工作,至少对于通过 registerNib:... 从 nib 文件加载的单元格而言是这样.

If you remove that property definition, selection and deselection works as expected, at least for the cells loaded from the nib file via registerNib:....

对于通过registerClass:... 加载的单元格,initWithFrame 被调用.您尝试从那里的 nib 文件加载单元格.这没有多大意义,似乎无法正常工作.您应该在 initWithFrame 中以编程方式创建单元并使用 registerClass:,或者在 nib 文件中创建单元并使用 registerNib:.

For the cells loaded via registerClass:..., initWithFrame is called. You try to load the cell from the nib file there. That does not make much sense and seems not to work correctly. You should either create the cell programmatically in initWithFrame and use registerClass:, or create the cell in the nib file and use registerNib:.

initWithFrame 不会为从 nib 文件加载的单元格调用,如果您想对单元格进行修改,请使用 awakeFromNib.

initWithFrame is not called for cells loaded from the nib file, use awakeFromNib if you want to make modifications to the cell.

希望有帮助!!

这篇关于子类化 UICollectionViewCell 导致永远不会被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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