[UICollectionViewCell setItemDictionary:]:无法识别的选择器发送到实例

时间:2016-01-02 11:46:01

标签: objective-c uiview uicollectionview

我有一个View Controller,在那个View Controller中,我有一个按钮。单击该按钮将在视图中滑动(ToolOptionsView)。在此视图中,我添加了一个集合视图,但我遇到此错误

  

[UICollectionViewCell setItemDictionary:]:无法识别的选择器发送到实例

以下是我的ToolOptionsView.m文件 -

#import "ToolOptionsView.h"

@interface ToolOptionsView ()

@property(nonatomic, strong) NSMutableArray *toolsList;
@property(nonatomic, strong) NSMutableDictionary *tools;

@end

@implementation ToolOptionsView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        [self setToolsList];

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

        self.toolsCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:flowLayout];
        self.toolsCollectionView.delegate = self;
        self.toolsCollectionView.dataSource=self;
        [self.toolsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"toolsCell"];

        [self addSubview:self.toolsCollectionView];

    }
    return self;
}


-(void)setToolsList{
    self.toolsList =[[NSMutableArray alloc]init];

    self.tools =[[NSMutableDictionary alloc]init];
    [self.tools setObject:@"Food" forKey:@"title"];
    [self.tools setObject:@"food.png" forKey:@"iconImage"];

    [self.toolsList addObject:self.tools];

    self.tools =[[NSMutableDictionary alloc]init];
    [self.tools setObject:@"Drinks" forKey:@"title"];
    [self.tools setObject:@"drinks.png" forKey:@"iconImage"];

    [self.toolsList addObject:self.tools];

    self.tools =[[NSMutableDictionary alloc]init];
   [self.tools setObject:@"Near By Restaurants" forKey:@"title"];
    [self.tools setObject:@"Restaurants.png" forKey:@"iconImage"];

    [self.toolsList addObject:self.tools];     
}

#pragma mark-
#pragma mark- UICollectionViewDatasource method

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

   return [self.toolsList count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MenuItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"toolsCell" forIndexPath:indexPath];
    NSMutableDictionary *dict = [self.toolsList objectAtIndex:indexPath.row];
    cell.itemDictionary = dict;
    return cell;
}
@end

这是我的MenuItemCollectionViewCell.h -

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>

@interface MenuItemCollectionViewCell : UICollectionViewCell

@property(nonatomic, strong)  UIImageView *iconImageView;
@property(nonatomic, strong)  UILabel *iconLabel;

@property(nonatomic, strong) NSMutableDictionary *itemDictionary;

@end

这是我的MenuItemCollectionViewCell.m文件 -

#import "MenuItemCollectionViewCell.h"

@implementation MenuItemCollectionViewCell

-(void)awakeFromNib{
    self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, self.bounds.size.width-40, self.bounds.size.height-40)];
    self.iconImageView.image = [UIImage imageNamed:[self.itemDictionary objectForKey:@"iconImage"]];
    [self addSubview:self.iconImageView];
    self.iconLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 23 + self.iconImageView.bounds.size.height, self.bounds.size.width-10 , 16)];
    self.iconLabel.text = [self.itemDictionary objectForKey:@"title"];
    [self addSubview:self.iconLabel];
}

@end

2 个答案:

答案 0 :(得分:2)

在第

[self.toolsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"toolsCell"];

您为标识符注册了课程UICollectionViewCell,但实际上应该注册您的自定义课程MenuItemCollectionViewCell

[self.toolsCollectionView registerClass:[MenuItemCollectionViewCell class] forCellWithReuseIdentifier:@"toolsCell"];

答案 1 :(得分:0)

很可能从dequeResuableCellWithIdentifier返回的单元格不是MenuItemCollectionViewCell 在该方法之后放置断点并检查调试器中返回的单元格,只需写入po cell.class