기본적으로 나는이 자습서 다음입니다 : http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html 어떤 이유로 마지막 셀 올바른 정보하지만 이전의 것들 제목 만 표시 표시만 마지막 jQuery과 셀은 정확, 나머지 쇼 널 (null) 항목
을또한 콘솔 창의 출력에는 작성자에 대한 정보 정보가 표시됩니다. 어떤 이유로 다른 세포로 끌어 당겨지지 않습니다. 왜, 어떻게이 문제를 해결하려면
#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
아이디어 : 여기
는하는 .m 파일입니다? 사전에 감사드립니다 :)편집 : 제공하는 답변 덕분에이 솔루션을 해결 한 후 : Martin R : dictionaryWithObjectsAndKeys 값 중 하나라도 nil 인 경우 어떻게 기본값을 제공합니까? 당신의 JSON 배열의
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
permalink_data,permalink,
nil];
많은 개체가 더 "썸네일"항목이없고, thumbnail_data
는 nil
입니다 :
왜'NSMutableString' : 값이
NSNull
인지 아닌지그런 다음,
cellForRowAtIndexPath
, 당신은 확인? – Larme당신이 직접 할 수 있다고 생각합니다 : cell.detailTextLabel.text = [tmpDict objectForKey : author]; – Ilario
@Larme 튜토리얼 – MarkP