UITableViewCell在单元格项目上设置文本时生成异常

时间:2013-03-08 11:22:38

标签: uitableview nsdictionary

我在cellForRowAtIndexPath中使用以下代码,当我在UILabel中填充数据时发送错误

NSDictionary *benchmarkDictionary = (NSDictionary *)[benchmarkArray objectAtIndex:indexPath.row];

((UILabel *)[cell viewWithTag:15]).text = [benchmarkDictionary objectForKey:@"benchmarkTitle"];
((UILabel *)[cell viewWithTag:16]).text = [benchmarkDictionary objectForKey:@"benchmarkDescription"];

例外情况是: - [Benchmark objectForKey:]:无法识别的选择器发送到实例0x75d59c0 2013-03-08 16:21:39.515 Fdtrainer [2719:11303] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [Benchmark objectForKey:]:无法识别的选择器发送到实例0x75d59c0 “

我的Benchmark课程是:

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

@implementation Benchmark

@synthesize bid;
@synthesize benchmarkTitle;
@synthesize benchmarkDescription;

- (id) initWithCoder:(NSCoder *)coder {

    self = [super init];
    if(self) {
        bid = [coder decodeObjectForKey:@"id"];
        benchmarkTitle = [coder decodeObjectForKey:@"benchmarkTitle"];
        benchmarkDescription = [coder decodeObjectForKey:@"benchmarkDescription"];
    }
    return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:bid forKey:@"id"];
    [encoder encodeObject:benchmarkTitle forKey:@"benchmarkTitle"];
    [encoder encodeObject:benchmarkDescription forKey:@"benchmarkDescription"];
}

@end

2 个答案:

答案 0 :(得分:0)

我猜你是以错误的方式访问你的房产:

试试这个:

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

@implementation Benchmark

@synthesize bid = _bid;
@synthesize benchmarkTitle = _benchmarkTitle;
@synthesize benchmarkDescription = _benchmarkDescription;

- (id) initWithCoder:(NSCoder *)coder {

self = [super init];
if(self) {
    _bid = [coder decodeObjectForKey:@"id"];
    _benchmarkTitle = [coder decodeObjectForKey:@"benchmarkTitle"];
    _benchmarkDescription = [coder decodeObjectForKey:@"benchmarkDescription"];
}
return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

[encoder encodeObject:_bid forKey:@"id"];
[encoder encodeObject:_benchmarkTitle forKey:@"benchmarkTitle"];
[encoder encodeObject:_benchmarkDescription forKey:@"benchmarkDescription"];
}

答案 1 :(得分:0)

以下行似乎可能导致问题。

NSDictionary *benchmarkDictionary = (NSDictionary *)[benchmarkArray objectAtIndex:indexPath.row];

你确定benchmarkAray里面包含NSDictionary吗?向NSDictionary投射内容不会使其成为一个。看起来chrisArray包含Benchmark对象,它是NSObject类型的,它们不响应objectForKey选择器。