0

I managed to parse data from YouTube API on UITable view using AFNetworking but i cant figure out how to use

didSelectRowAtIndexPath:(NSIndexPath *)indexPath

How do i pass the JSON data to the next view ?

Here is the link to the file: google drive

Here is my code

#import "KKViewController.h"

#import "AFNetworking.h"


#import "AFJSONRequestOperation.h"

@interface KKViewController ()

@end

@implementation KKViewController

@synthesize  movies = _movies, count = _count;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {



        self.movies = [JSON valueForKeyPath:@"data.items.video"];

        // NSLog(@@, video)

        //


        self.count = [JSON valueForKeyPath:@"data.items.video.thumbnail"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];





    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (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.

    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }


}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];


    NSDictionary *countt = [self.count objectAtIndex:indexPath.row];
    cell.textLabel.text = [movie objectForKey:@"title"];
    cell.detailTextLabel.text = [movie objectForKey:@"uploader"];

    NSURL *url = [[NSURL alloc] initWithString:[countt objectForKey:@"hqDefault"]];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}
user1951876
  • 294
  • 2
  • 5
  • 16

2 Answers2

1

You could either:

  1. Define a segue in your storyboard's prototype for the cell in question and you don't have to write a didSelectRowAtIndexPath at all;

  2. Have didSelectRowAtIndexPath do a performSegueWithIdentifier (giving it the unique string "storyboard identifier" that you set up for your segue in the storyboard;

You can then write a prepareSegue that would look at the table's indexPathForSelectedRow to identify what row was selected. You can then grab the appropriate information from your app's model and set the appropriate property in the destinationViewController.

You could also have didSelectRowAtIndexPath do an instantiateViewControllerWithIdentifier set the property, and then do the appropriate pushViewController or presentViewController, but I personally prefer the above techniques that leverage the segues you've defined in your storyboard.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • i defined a segue. but how do i pass the JSON data associated with that cell. I am parsing video titles from a youtube channel. I want to pass the description to the next view. – user1951876 Feb 06 '13 at 03:18
  • 1
    1. Grab indexPath.row via `indexPathForSelectedRow`; 2. `NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];`; 3. `NSString *title = [movie objectForKey:@"title"];`; 4. pass `title` to your destinationController. Do you have a `@property` in the destination controller` to hold the `title`? – Rob Feb 06 '13 at 03:33
  • @user1951876 I've sent you a [pull request](https://github.com/miznat/youtube/pull/1) that illustrates my previous point. The only difference is that, instead of just passing the title, I passed the whole `NSDictionary` entry associated for that tableview row. In terms of discussion of how to pass data between controllers, see http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers. – Rob Feb 07 '13 at 14:13
1

I hope this sample code will help you to accomplish your needs easily. Please ask if anything look ambiguous to you.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
 {
  [self.leadsTableView deselectRowAtIndexPath:indexPath animated:YES];
  id object = [[self.tableData objectAtIndex:indexPath.section]    objectAtIndex:indexPath.row];
self.leadforLeadDetails = (Lead*)object;

self.selectedLeadID = self.leadforLeadDetails.ID;
[self performSegueWithIdentifier:@"openLeadDetails" sender:self];


// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}


 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
   //NSIndexPath *indexPath = [self.tableView2 indexPathForCell:sender];
   //[self.tableView2 deselectRowAtIndexPath:indexPath animated:YES];
   if ([segue.identifier isEqualToString:@"openLeadDetails"])
{
       LeadsDetailsTableViewController *destViewController = segue.destinationViewController;
       destViewController.dictLeadDetails = self.dictLeadDetails;

       destViewController.leadID = self.leadforLeadDetails.ID;
       destViewController.Name = self.leadforLeadDetails.Name;
       destViewController.leadDetails.ID = self.leadforLeadDetails.ID;
       destViewController.leadDetails.Name = self.leadforLeadDetails.Name;
     }
 }