“无法识别的选择器发送到实例”设置UILabel的文本时

时间:2014-04-02 02:39:10

标签: ios objective-c xcode exception

我是iOS编程的学生,通过Big Nerd Ranch出版。我看到很多其他人都发现了同样的异常“无法识别的选择器发送到实例”,但无法找到导致它在我的代码中的原因。

这种方法正在我尝试设置UITableViewCell的UILabel的文本。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BNRItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BNRItemCell" forIndexPath:indexPath];
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    //Set BNRItem instances to equal a given object in the items[] array
    BNRItem *item = items[indexPath.row];
    cell.nameLabel.text = item.itemName;  //This is the line where the exception happens
    cell.serialNumberLabel.text = item.serialNumber;
    cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];
    cell.thumbnailView.image = item.thumbNail;

    return cell;

}

在BNRItem实现中,我使用归档来存储其属性的值(用于在文本标签中设置文本,这是抛出异常的地方)。

-(void) encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.itemName forKey:@"itemName"];
    [aCoder encodeObject:self.serialNumber forKey:@"serialNumber"];
    [aCoder encodeObject:self.dateCreated forKey:@"dateCreated"];
    [aCoder encodeObject:self.itemKey forKey:@"itemKey"];
    [aCoder encodeInt:self.valueInDollars forKey:@"valueInDollars"];
    [aCoder encodeObject:self.thumbNail forKey:@"thumbnail"];
}



- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self)
    {
        _itemName = [aDecoder decodeObjectForKey:@"itemName"];
        _serialNumber= [aDecoder decodeObjectForKey:@"serialNumber"];
        _dateCreated = [aDecoder decodeObjectForKey:@"dateCreated"];
        _itemKey = [aDecoder decodeObjectForKey:@"itemKey"];
        _valueInDollars = [aDecoder decodeIntForKey:@"valueInDollars"];
        _thumbNail = [aDecoder decodeObjectForKey:@"thumbnail"];


    }
    return self;
}



- (id) initWithItemName:(NSString *)name
         valueInDollars: (int) value
           serialNumber: (NSString *) sNumber;
    {

    self = [super init];


    //Give the instance variables initial values
    [self setItemName:name]; //point to self since its the initializer of the object
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    _dateCreated = [[NSDate alloc]init];

    NSUUID *uuid = [[NSUUID alloc] init];
    NSString *key = [uuid UUIDString];
    _itemKey = key;

    return self; //return self so that you can assign it to a variable
}

编辑 - 这是BNRItem的其余实现。即,一种随机化BNRItem属性的方法,另一种方法是给它一个缩略图

//Creates a BNRItem instance with a random name, random value and random serial number
+ (id)randomItem {
//Create an array of three adjectives
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"John", @"Kyle", @"Jerry", nil];

//Create an array of three nouns
NSArray *randomNounList = [NSArray arrayWithObjects:@"Cheese", @"Meat", @"Vegetables",    
nil];

NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
NSInteger nounIndex = rand() % [randomNounList count];

NSString *randomName = [NSString stringWithFormat:@"%@ %@", [randomAdjectiveList   
objectAtIndex:adjectiveIndex], [randomNounList objectAtIndex:nounIndex]];

int randomValue = rand () % 100;

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10 ];

BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                   valueInDollars:randomValue
                                     serialNumber:randomSerialNumber];

return newItem;

}




- (void) setThumbNailFromImage:(UIImage *)image
{
CGSize origImageSize = image.size;

CGRect newRect = CGRectMake(0, 0, 40, 40);

//Figure out a scaling ratio to make sure we maintain the same aspect ratio
float ratio = MAX(newRect.size.width / origImageSize.width, newRect.size.height /  
origImageSize.height);

//Createa a transparent bitmap context with a scaling factor equal to that of the screen

UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

//Create a path that is a rounded rectangle
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];

//Make all subsequent drawing clip to this rounded rectangle
[path addClip];

//Center the image in the thumbnail rectangle
CGRect projectRect;
projectRect.size.width = ratio * origImageSize.width;
projectRect.size.height  = ratio * origImageSize.height;
projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height);


//Draw the image on it
[image drawInRect:projectRect];

//Get the image from the image context; keep it as our thumbnail
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
self.thumbNail = smallImage;

//Clea image context resources; we're done
UIGraphicsEndImageContext();


}

UITableViewCell是使用xib创建的,有4个插座(3个标签和1个图像)。

#import <Foundation/Foundation.h>

@interface BNRItemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *thumbnailView;

@end

我还没有学过Core Data但是它已经出现了,问题是我需要得到我的工作,以便我可以继续修改教科书指令。任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:0)

;之后你有一个额外的serialNumber: (NSString *) sNumber;导致错误。删除它&amp;再试一次。

答案 1 :(得分:0)

试试这可能会对你有所帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

 BNRItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BNRItemCell"  forIndexPath:indexPath];
if (cell == nil)
{
 cell = [[BNRItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSArray *items = [[BNRItemStore sharedStore] allItems];
//Set BNRItem instances to equal a given object in the items[] array
BNRItem *item = [items objectAtIndex:indexPath.row];// try this as well
cell.nameLabel.text = [NSString stringWithFormat:@"%@",item.itemName];  // must try this
cell.serialNumberLabel.text = item.serialNumber;
cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];
cell.thumbnailView.image = item.thumbNail;

return cell;

}

答案 2 :(得分:0)

我也遇到了这个问题。我进入了BNRItemCell.xib文件并打开了文档大纲,右键单击视图树中的一些标签以查看参考出口。我注意到他们已经连接到xib中的文件所有者以及BNRItemCell.h文件中的IBOutlet。我从文件所有者那里取消了它们并修复了问题。不确定这是否是您的问题,但请查看。