的UITableView在正确

的UITableView在正确

问题描述:

编辑 - 解决不拉JSON: 我添加了对了,我有映射问题的关怀“频道”模型。谢谢!的UITableView在正确

我想UITableView行来显示所有的 “标题”,但我得到一个崩溃:

WebListViewController.m -

@interface WebListViewController() 

@property (strong, nonatomic) NSArray *headlinesNow; 

@end 

@implementation WebListViewController 

@synthesize tableView = _tableView; 
@synthesize headlinesNow; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *lAbbreviation = [defaults objectForKey:@"lAbbreviation1"]; 
    NSString *tID = [defaults objectForKey:@"tId1"]; 

NSString *url = [NSString stringWithFormat:@"/now/?l=%@&t=%@&apikey=5xxxxxxxx", lAbbreviation, tID]; 
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader) { 
     loader.onDidLoadObjects = ^(NSArray *objects) { 

headlinesNow = objects; 

[_tableView reloadData]; 

[loader.mappingProvider setMapping:[Headline mapping] forKeyPath:@"feed.headline"]; 
     loader.onDidLoadResponse = ^(RKResponse *response){ 
      NSLog(@"BodyAsString: %@", [response bodyAsString]); 
     }; 
    }]; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return headlinesNow.count; 
} 

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

    static NSString *CellIdentifier = @"standardCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    Headline *headlineLocal = [headlinesNow objectAtIndex:indexPath.row]; 
    NSString *headlineText = [NSString stringWithFormat:@"%@", headlineLocal.headline]; 
    cell.textLabel.text = headlineText; 
    return cell; 
} 

Headline.h

@interface Headline : NSObject 
@property (strong, nonatomic) NSString *headline; 
@property (strong, nonatomic) Links *linksHeadline; 
+ (RKObjectMapping *) mapping; 
@end 

Headline.m

@implementation Headline 
+ (RKObjectMapping *)mapping { 
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) { 
     [mapping mapKeyPathsToAttributes: 
     @"headline", @"headline", 
     nil]; 
     [mapping hasOne:@"links" withMapping:[Links mapping]]; 
    }]; 
    return objectMapping; 
} 
@end 

JSON响应体 -

{ 
    "timestamp": "2013-05-03T22:03:45Z", 
    "resultsOffset": 0, 
    "status": "success", 
    "resultsLimit": 10, 
    "breakingNews": [], 
    "resultsCount": 341, 
    "feed": [{ 
     "headline": "This is the first headline", 
     "lastModified": "2013-05-03T21:33:32Z", 
     "premium": false, 
     "links": { 
      "api": { 

我得到了NSLog输出bodyAsResponse,但该程序崩溃,并在UITableView不灌装。我显然没有正确导航到JSON?有任何想法吗?

欣赏的帮助,让我知道,如果你需要更多的代码snippets-

编辑 - 崩溃是: 终止应用程序由于未捕获的异常 'NSUnknownKeyException',原因:“[< __NSCFString 0xe693110> valueForUndefinedKey:]:该类不是关键标题的关键字编码标准。“

+0

什么是崩溃? – Wain 2013-05-03 22:19:17

+0

此外,更好地显示您的映射配置比表视图和空的init方法代码。 – Wain 2013-05-03 22:20:27

+0

@Wain感谢您的提示,我添加了崩溃,并根据您的建议添加了映射 - – Realinstomp 2013-05-03 22:25:30

第一个问题是您当前的forKeyPath:@"feed.headline"。它应该是forKeyPath:@"feed",因为该keypath对于您的JSON无效。原因是你的JSON中的feed是一个数组,所以它应该映射到你的Headline对象,那么你的对象的映射定义将用于headline

下一个问题是linksHeadline属性和[mapping hasOne:@"links" withMapping:[Links mapping]];。键名不匹配。将Headline中的属性名称更改为links

+0

非常感谢! – Realinstomp 2013-05-04 19:29:21