Ios &引用;已将无法识别的选择器发送到实例";设置UILabel时';s文本

Ios &引用;已将无法识别的选择器发送到实例";设置UILabel时';s文本,ios,objective-c,xcode,exception,Ios,Objective C,Xcode,Exception,我是一名学习iOS编程的学生,正在阅读《大书呆子农场》一书。我看到很多其他人都发现了相同的异常“未识别的选择器发送到实例”,但在我的代码中找不到导致该异常的原因 在这个方法中,我尝试设置UITableViewCell的UILabel的文本 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BNRItemCell *cell =

我是一名学习iOS编程的学生,正在阅读《大书呆子农场》一书。我看到很多其他人都发现了相同的异常“未识别的选择器发送到实例”,但在我的代码中找不到导致该异常的原因

在这个方法中,我尝试设置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;

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

编辑-以下是bRitem的其余实现。也就是说,一种方法是对bNitem的属性进行随机化,另一种方法是给它一个缩略图

//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个标签和一个图像)

#导入
@接口BNRItemCell:UITableViewCell
@属性(弱,非原子)IBUILabel*serialNumberLabel;
@属性(弱、非原子)IBUILabel*valueLabel;
@属性(弱,非原子)IBUILabel*nameLabel;
@属性(弱、非原子)IBUIImageView*thumbnailView;
@结束

我还没有学习核心数据,但它即将出现,问题是我需要得到我所拥有的工作,以便我可以继续修改它与教科书的指示。谢谢你的帮助

您有一个额外的
序列号之后:(NSString*)sNumber这会导致错误。删除它并重试。

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

- (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;

}

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

bRitemCell是否有UILabel类型的serialNumberLabel属性?是的。我正在编辑OP并为bRitemCell-1添加.h,因为它没有引用准确完整的错误消息。正如@HotLicks指出的:请向我们展示完整的错误消息。问题可能出在你的
b脚本中吗?
?你知道你是在哪一行出错的吗?多亏了敏锐的目光,我摆脱了它,但还是出错了。你在情节提要中链接了
UITableView
IBOutlet
了吗?不,不是在情节提要中。我刚刚从xib中拖动了控件。我假设您已经导入了
UIKit
,对吗?我看到你包括了
Foundation
,但没有包括
UIKit
(好吧,但是你可以运行这个应用程序,所以你可能已经运行了)。尝试逐行注释代码以定位错误YUP,当我注释掉将UITableViewCell的标签指向BNRItem属性的行时,错误消失。这肯定与bNitem类有关,我发布了该类的其余实现。啊,仍然得到错误:(.具体消息是“2014-04-02 15:30:43.352 Homepwner[34104:90b]-[\uu NSCFString itemName]:发送到实例0x10951fe00的无法识别的选择器”啊,它仍然不起作用。代码也是直接从BNR教科书中复制的,我假设代码本身没有功能失调。可能是文件的存储方式导致的吗?谢谢你的帮助。问题是你的itemname没有任何值,你正在将其文本设置为label,这就是为什么它给了你那条信息不幸的是,我放弃了它,我有一个课程的最后作业应用程序,所以我跳了进去,把BNR教程抛在了脑后。不过谢谢你的回复,如果我以后遇到这个问题,它绝对没有任何价值。
#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
- (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;

}