2013-05-16 3 views
0

Я начинаю пытаться научиться разбирать JSON, используя NSJSONSerialization в iOS 6.1. Я занимаюсь Twitter API и пытаюсь получить твитовские тенденции api, я могу получить тенденции Twitter в моем TableView, но когда я пытаюсь передать данные между viewcontrollers, чтобы показать подробное представление, я не получаю никаких данных, здесь мой код,DetailViewController, не отображающий данные для Twitter Тенденции

#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; } возвращение [self initwithURL: url]; } – Shreek

+0

Вы не инициализируете DetailViewController правильно. Где вызов [super init]? – Amar

ответ

0

используется array или names для хранения URL-адресов?

Попробуйте это в didselectRow .. метод

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

Я хранил в массиве, но его не помогло :( – Shreek

+0

Его решила, я использовал быстрое перечисление, но это не было необходимо. Спасибо, ребята! – Shreek

Смежные вопросы