基本上我遵循本教程:http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html由于某种原因,最后一个单元格显示正确的信息,但之前的单元格只显示标题:
此外,控制台窗口中的输出显示作者的信息信息。因此,由于某种原因,它没有被拉入其他细胞。
这是.m文件:
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *myObject;
// A dictionary object
NSDictionary *dictionary;
// Define keys
NSString *title;
NSString *thumbnail;
NSString *author;
NSString *permalink;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
title = @"title";
thumbnail = @"thumbnail";
author = @"author";
permalink = @"permalink";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://ios-blog.co.uk/?feed=json"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:@"title"];
NSString *thumbnail_data = [dataDict objectForKey:@"thumbnail"];
NSString *author_data = [dataDict objectForKey:@"author"];
NSString *permalink_data = [dataDict objectForKey:@"permalink"];
NSLog(@"TITLE: %@",title_data);
NSLog(@"THUMBNAIL: %@",thumbnail_data);
NSLog(@"AUTHOR: %@",author_data);
NSLog(@"URL: %@",permalink_data);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
permalink_data,permalink,
nil];
[myObject addObject:dictionary];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *text;
//text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:title]];
text = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKeyedSubscript:title]];
NSMutableString *detail;
detail = [NSMutableString stringWithFormat:@"Author: %@ ",
[tmpDict objectForKey:author]];
NSMutableString *images;
images = [NSMutableString stringWithFormat:@"%@ ",
[tmpDict objectForKey:thumbnail]];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
cell.imageView.image =img;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"URL: %@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);
NSString* launchUrl = (@"%@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
任何想法为什么以及如何解决这个问题?在此先感谢:)
编辑:解决此解决方案后,由于提供的答案:Martin R:如果任何dictionaryWithObjectsAndKeys值为nil,我如何提供默认值?
答案 0 :(得分:3)
问题在于:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
permalink_data,permalink,
nil];
JSON数组中的许多对象没有“缩略图”条目,thumbnail_data
为nil
。
但nil
充当dictionaryWithObjectsAndKeys:
的终结者,
因此,以下键“作者”和“永久链接”将被忽略。
有多种方法可以解决这个问题。例如,你可以
使用dictionaryWithValuesForKeys:
代替dictionaryWithObjectsAndKeys:
:
for (NSDictionary *dataDict in jsonObjects) {
NSDictionary *dictionary = [dataDict dictionaryWithValuesForKeys:@[title, thumbnail, author, permalink]];
[myObject addObject:dictionary];
}
此方法会将NSNull
值替换为nil
值(并且代码更少)。
然后,在cellForRowAtIndexPath
中,检查值是否为NSNull
:
NSDictionary *tmpDict = dataDict[indexPath.row];
NSString *thumbnailURL = tmpDict[thumbnail];
UIImage *img;
if ([thumbnailURL isEqual:[NSNull null]]) {
img = [UIImage imageNamed:@"DefaultImage.png"];
} else {
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
}