簡體   English   中英

DetailViewController不顯示Twitter趨勢的數據

[英]DetailViewController not showing the data for Twitter Trends

我是一個初學者,試圖學習在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

您使用arraynames來存儲網址嗎?

在didselectRow ..方法中嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM