DetailViewController 未显示 Twitter 趋势数据

问题描述 投票:0回答:1

我是一个初学者,试图学习在 iOS 6.1 中使用

NSJSONSerialization
解析 JSON。我正在修改 Twitter API 并尝试检索 Twitter 趋势 API,我能够在 TableView 中检索 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
ios json twitter nsjsonserialization
1个回答
0
投票

您使用

array
names
来存储网址?

在 didselectRow.. 方法中尝试这个

NSURL *url  = [NSURL URLWithString:[array objectAtIndex: indexPath.row]];
© www.soinside.com 2019 - 2024. All rights reserved.