2014-01-28 1 views
0

기본적으로 나는이 자습서 다음입니다 : http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html 어떤 이유로 마지막 셀 올바른 정보하지만 이전의 것들 제목 만 표시 표시만 마지막 jQuery과 셀은 정확, 나머지 쇼 널 (null) 항목

screenshot of ios app

또한 콘솔 창의 출력에는 작성자에 대한 정보 정보가 표시됩니다. 어떤 이유로 다른 세포로 끌어 당겨지지 않습니다. 왜, 어떻게이 문제를 해결하려면

#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_datanil입니다 :

+0

왜'NSMutableString' : 값이 NSNull인지 아닌지

그런 다음, cellForRowAtIndexPath, 당신은 확인? – Larme

+1

당신이 직접 할 수 있다고 생각합니다 : cell.detailTextLabel.text = [tmpDict objectForKey : author]; – Ilario

+0

@Larme 튜토리얼 – MarkP

답변

3

문제는 여기에있다. dictionaryWithObjectsAndKeys:에 대한 종료

그러나 nil 행위, 따라서 다음 키 "작가"와 "영구 링크"는 무시됩니다.

이 문제를 해결하는 방법은 다양합니다. 예를 들어, dictionaryWithObjectsAndKeys: 대신 dictionaryWithValuesForKeys:을 사용할 수

for (NSDictionary *dataDict in jsonObjects) { 
    NSDictionary *dictionary = [dataDict dictionaryWithValuesForKeys:@[title, thumbnail, author, permalink]]; 
    [myObject addObject:dictionary]; 
} 

이 방법은 nilNSNull 값을 대체합니다 (그리고 적은 코드입니다).

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]; 
} 
+0

굉장합니다. 지정되지 않은 경우 기본 미리보기 이미지를 어떻게 사용합니까? 미안 해요, 초보자예요. – MarkP

+0

나는 이것을 해결할 수 있다고 생각한다. 직접 할 수있다. cell.detailText.text = [tmpDic objectForKey : author]; 권리? – Ilario

+0

@Ilario : 그렇게 할 수는 있지만 문제는 더 일찍 발생합니다. –

관련 문제