2013-05-16 3 views
0

저는 iOS 6.1에서 NSJSONSerialization을 사용하여 JSON을 파싱하는 법을 배우는 초보자입니다. 트위터 API로 어지럽히고 트위터 동향 api를 검색하려고하는데 뷰 뷰어간에 트위터 트렌드를 가져올 수 있지만 viewController간에 데이터를 전달하려고 할 때 자세히보기를 표시하지 않고 데이터가 없습니다. 내 코드, 당신은 URL을 저장하는 array 또는 names를 사용DetailViewController 트위터 트렌드에 대한 데이터가 표시되지 않습니다.

#import "ViewController.h" 

    #import "DetailViewController.h" 

    @interface ViewController() 

    { 
     NSMutableData *webData; 

     NSURLConnection *connection; 

     NSMutableArray *array; 
    } 

    @end 

    @implementation ViewController 
    @synthesize names; 
    @synthesize ServiceView; 
    @synthesize urls; 

    - (void)viewDidLoad 
    { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 


array = [[NSMutableArray alloc]init]; 


NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/trends/1.json"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

if (connection) { 
    webData = [[NSMutableData alloc]init]; 
} 

self.title = @"Twitter Trends"; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
    } 

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 


     [webData setLength:0]; 

    } 

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

     [webData appendData:data]; 

    } 

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
     NSLog(@"Did fail with error"); 
    } 


    -(void)connectionDidFinishLoading:(NSURLConnection *)connection { 

     NSArray *twitterTrends = [NSJSONSerialization JSONObjectWithData:webData options:0  error:nil]; 
     NSArray *trends = [[twitterTrends objectAtIndex:0] objectForKey:@"trends"]; 


     for (NSDictionary *trend in trends) { 


      NSDictionary *names = [trend objectForKey:@"name"]; 

      NSDictionary *urls = [trend objectForKey:@"urls"]; 

      //NSString *label = [title objectForKey:@"label"]; 

      [array addObject:names]; 


     } 



     [[self ServiceView]reloadData]; 



     } 

     #pragma Table View Methods 


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
     { 
     // Return the number of sections. 
      return 1; 
     } 

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

// Return the number of rows in the section. 
return array.count; 
     } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
     static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.textLabel.text = [array objectAtIndex:indexPath.row]; 

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

return cell; 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

// NSString *title = [names objectAtIndex:indexPath.row]; 
//NSURL *url = [NSURL URLWithString:@"http://www.apple.com"]; 
NSURL *url = [NSURL URLWithString:[names objectAtIndex: indexPath.row]]; 

DetailViewController *DVC = [[DetailViewController alloc]initwithURL:url]; 



[self.navigationController pushViewController:DVC animated:YES]; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
     } 

     @end 
+0

당신은'DetailViewController'의'initwithURL에서 할 무엇을이 시도? – Amar

+0

- (id) initwithURL : (NSURL *) url { if (self) { theURL = url; } return [self initwithURL : url]; } – Shreek

+0

DetailViewController를 올바르게 초기화하지 않습니다. [super init]에 대한 호출은 어디에 있습니까? – Amar

답변

0

은? `방법 :

방법 .. didselectRow에

NSURL *url = [NSURL URLWithString:[array objectAtIndex: indexPath.row]]; 
+0

배열에 저장하고 있지만 도움이되지 않습니다. ( – Shreek

+0

해결되었지만 빠른 열거 형을 사용했지만 필요하지 않았습니다. 감사합니다! – Shreek