简体   繁体   中英

DetailViewController not showing the data for Twitter Trends

I am a beginner trying to learn to parse JSON using NSJSONSerialization in iOS 6.1. I am tinkering with Twitter API and trying to retrieve the Twitter trends api, I am able to retrieve the Twitter trends in my TableView however when I am trying to pass the data between viewcontrollers to show the detail view, I am getting no data, here is my code,

    #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

You use array or names to store urls?

Try this in didselectRow.. method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM