我有一個視圖控制器,並在該視圖控制器,我有一個按鈕。單擊該按鈕將在視圖中滑動(ToolOptionsView)。在該視圖中我有一個集合視圖中添加,但我有這個錯誤[UICollectionViewCell setItemDictionary:]:無法識別的選擇器發送到實例
[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
,這裏是從dequeResuableCellWithIdentifier
返回MenuItemCollectionViewCell
我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
您是否正確設置了標識符和類?看起來你正在獲得一個通用的'UICollectionViewCell'而不是'MenuItemCollectionViewCell'。 – luk2302
謝謝,這是問題所在。我註冊了我的標識符UiCollectionViewCell,而我應該爲MenuItemCollectionViewCell完成。你可以請你的建議作爲答覆,以便我可以接受它。 – Natasha