iOS-如何解决发送到实例

时间:2018-04-30 07:37:16

标签: ios objective-c uicollectionviewcell

我将UICollectionViewCell子类化,用gridItem填充用户界面

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [__ NSCFDictionary gridTitle]:   无法识别的选择器发送到实例0x1c4a76ec0'            ***第一次抛出调用堆栈:            (0x1829dad8c 0x181b945ec 0x1829e8098 0x1829e05c8 0x1828c641c 0x100e8712c 0x100e8a4f0 0x18c701fb8 0x18c6e7454 0x18c6e0790   0x18c5af770 0x186b5125c 0x186b553ec 0x186ac1aa0 0x186ae95d0   0x186aea450 0x182982910 0x182980238 0x182980884 0x1828a0da8   0x184883020 0x18c88178c 0x100e6ae54 0x182331fc0)            libc ++ abi.dylib:以NSException类型的未捕获异常终止

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView 
cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UICollectionViewCell *gridCell = nil;
            HDZGoodsGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HDZGoodsGridCellID forIndexPath:indexPath];
            cell.gridItem = _gridItem[indexPath.row];
            cell.backgroundColor = [UIColor whiteColor];
            gridCell = cell;
    }
    return gridCell;
}

#import <UIKit/UIKit.h>
@class HDZGridItem;
@interface HDZGoodsGridCell : UICollectionViewCell

@property (strong , nonatomic)HDZGridItem *gridItem;

@end


#import "HDZGoodsGridCell.h"
#import "HDZGridItem.h"
#import "UIColor+HDZColorTranslation.h"
#import <UIImageView+WebCache.h>
@interface HDZGoodsGridCell()

/* imageView */
@property (strong , nonatomic)UIImageView *gridImageView;
/* gridLabel */
@property (strong , nonatomic)UILabel *gridLabel;
/* tagLabel */
@property (strong , nonatomic)UILabel *tagLabel;

@end
@implementation HDZGoodsGridCell
#pragma mark - Setter Getter Methods
- (void)setGridItem:(HDZGridItem *)gridItem
{
    _gridItem = gridItem;
    // !!! The following line of code crashes !!!
    _gridLabel.text = gridItem.gridTitle;


    _gridLabel.text = gridItem.gridTitle;
    _tagLabel.text = gridItem.gridTag;
    _tagLabel.hidden = (gridItem.gridTag.length == 0) ? YES : NO;
    if (_gridItem.iconImage.length == 0) return;
    if ([[_gridItem.iconImage substringToIndex:4] isEqualToString:@"http"]) {
        [_gridImageView sd_setImageWithURL:[NSURL URLWithString:gridItem.iconImage]placeholderImage:[UIImage imageNamed:@"default_49_11"]];
    }else{
        _gridImageView.image = [UIImage imageNamed:_gridItem.iconImage];
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    _gridImageView = [[UIImageView alloc] init];
    _gridImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_gridImageView];

    _gridLabel = [[UILabel alloc] init];
    _gridLabel.font = Font13;
    _gridLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_gridLabel];

    _tagLabel = [[UILabel alloc] init];
    _tagLabel.font = [UIFont systemFontOfSize:8];
    _tagLabel.backgroundColor = [UIColor whiteColor];
    _tagLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_tagLabel];
}
@end

1 个答案:

答案 0 :(得分:-1)

  

&#39; NSInvalidArgumentException&#39;,原因:&#39; - [__ NSCFDictionary gridTitle]:无法识别的选择器发送到实例0x1c4a76ec0

表示您尝试在gridTitle上调用方法__NSCFDictionary,该方法只是Dictionary。现在,我们可以查看您尝试在代码中调用gridTitle的位置:在传递的参数setGridItemgridItem,它应该是HDZGridItem。这意味着在调用该方法时,您实际上传入的是Dictionary而不是HDZGridItem