【发布时间】:2014-04-02 02:39:10
【问题描述】:
我是一名 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 个标签和一个图像)。
#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,但它即将到来,问题是我需要得到我正在工作的东西,以便我可以根据教科书的说明继续修改它。任何帮助表示赞赏!
【问题讨论】:
-
BNRItemCell 是否有序列号标签,UILabel 类型的属性?
-
是的。我正在编辑 OP 并为 BNRItemCell 添加 .h
-
-1 没有引用准确和完整的错误信息。
-
正如@HotLicks 指出的:请向我们展示完整的错误消息。问题可能出在您的
BNRItem上吗? -
你知道你在哪一行得到这个错误吗??
标签: ios objective-c xcode exception