【问题标题】:Issue in unrecognized selector sent to instance发送到实例的无法识别选择器中的问题
【发布时间】:2013-01-23 03:07:51
【问题描述】:

我正在创建一个 CollectionView 应用程序。我通过 JSON 字符串从服务器获取数据。我的工作流程如下

  1. 创建的集合视图单元格为 ProjectVCollectionViewCell.h、.m、.xib。

    .h 的代码是 -

@interface ProjectCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *projectImage;
@property (weak, nonatomic) IBOutlet UILabel *projectLabel;
@结束

.m 的代码是默认的,并综合了上述 2 个变量。 并且 .xib 具有带有图像视图和标签的集合视图单元(将上述类与集合视图单元链接并将标识符名称链接为“projectCell”。

  1. 其ViewController的代码,包含collectionView,如下

ViewController.m 里面的代码是

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section{
    return [projectList count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    ProjectCollectionViewCell *cell = (ProjectCollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"projectCell" forIndexPath:indexPath];    

    cell.projectLabel.text = @"";//Here i am getting issue
    //cell.projectLabel.text = [[NSString alloc] initWithString:[[projectList objectAtIndex:indexPath.row] objectForKey:@"albumName"]];        
    return cell;
}

在上面的代码中 cell.projectlabel 给了我问题

[UICollectionViewCell projectLabel]: unrecognized selector sent to instance 0x75f0fb0
2013-02-07 20:08:17.723 Sample[3189:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell projectLabel]: unrecognized selector sent to instance 0x75f0fb0'
*** First throw call stack:

单元格值很好,我用 NSLog 跟踪到,代码提示也在“。”之后使用 projectLabel。但我无法通过这个设置标签字段值。所以请帮帮我。

【问题讨论】:

  • 你注册nib了吗?
  • 是的。如下 - [self.projectListView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"projectCell"];
  • @Sang 你真是个天才。 :)。你救了我的命。请将此作为您的答案。我会将其视为解决方案。

标签: uicollectionview uicollectionviewcell


【解决方案1】:

如果使用 ProjectCollectionViewCell.xib 创建自定义 ProjectCollectionViewCell,则必须在 viewDidLoad 中注册 Nib:

[self.projectListView registerNib:[UINib nibWithNibName:@"ProjectCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"projectCell"];

您应该改为使用 StoryBoard 来节省 CollectionView 的时间。不需要注册任何东西,只需选择customCell,将你想要的所有内容拖放到CollectionView单元格中并拖动IBOutlet:https://github.com/lequysang/testCollectionViewScroll

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多