Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Populating Table View with YouTube Thumnail using AFNetworking

AshtashooterAshtashooter MalaysiaPosts: 25 @
edited November 2014 in iOS SDK Development
Hello,

I have a table that I want to populate with YouTube thumbnail using AFNetworking. The problem is that I get nothing in my table at runtime. Just empty cells.

Please tell me what I'm doing wrong exactly.

Thank you!

Here is my code:
#import "ViewController.h"        
 #import "AFHTTPRequestOperationManager.h"
 #import "UIImageView+WebCache.h"


 @interface ViewController () {
        NSMutableArray *myObjects;
    }

    @end

    @implementation ViewController

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

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:@"http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?time=today&q=%@&max-results=16&alt=json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            myObjects = [[responseObject objectForKey:@data]objectForKey:@items];
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@Error: %@", error);
        }];

    }

    - (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.
        return myObjects.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @Cell;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        /*
        [cell.imageView setImageWithURL: [NSURL URLWithString: [[[myObjects objectAtIndex: indexPath.row] objectForKey: @thumbnail] objectForKey: @sqDefault]]
                       placeholderImage:[UIImage imageNamed:@placeholder.png]];
        */

        NSString *imageURLString;

        if (imageURLString && ![imageURLString isEqual:[NSNull null]])
        {
            NSURL *imageURL = [NSURL URLWithString:imageURLString];
            [cell.imageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@placeholder.png]];
        }

        cell.textLabel.text = [[myObjects objectAtIndex: indexPath.row] objectForKey: @title];

        return cell;
    }


    @end

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    Add some logs so that you can see what is happening. Ex:
    - (NSInteger)tableView:(UITableView *)tableView
       numberOfRowsInSection:(NSInteger)section
    {
       NSLog(@>>> Entering <%p> %s <<<, self, __PRETTY_FUNCTION__);
    
       NSLog(@Number of rows: %d, myObjects.count);
    
       NSLog(@<<< Leaving  <%p> %s >>>, self, __PRETTY_FUNCTION__);
       return myObjects.count;
    }
    
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    Thank you BrianSlick for your reply. I did what you suggested but nothing shows up in the output when I hit run. I don't know what's wrong with my code. Apparently, myObjects doesn't receive any data from AFNetworking.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    If nothing shows up with those particular logs, then you aren't even loading the table. Keep adding logs until you find the problem.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    Nothing showed up so I made some modifications to my code. I deleted the Table View Controller and created a View Controller, and added a Table View and a Table View Cell to it. Then, I made the required connections so now my table shows data but only text data. Why is there no thumbnail along with the text data?
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    Keep adding logs. This is basic troubleshooting.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    edited November 2014
    This is the output I get:

    2014-11-14 22:02:18.593 OK[2611:140791] >>> Entering <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] <<<
    2014-11-14 22:02:18.671 OK[2611:140791] Number of rows: 0
    2014-11-14 22:02:18.672 OK[2611:140791] <<< Leaving <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] >>>
    2014-11-14 22:02:18.674 OK[2611:140791] >>> Entering <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] <<<
    2014-11-14 22:02:18.674 OK[2611:140791] Number of rows: 0
    2014-11-14 22:02:18.674 OK[2611:140791] <<< Leaving <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] >>>
    2014-11-14 22:02:19.486 OK[2611:140791] >>> Entering <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] <<<
    2014-11-14 22:02:19.486 OK[2611:140791] Number of rows: 10
    2014-11-14 22:02:19.486 OK[2611:140791] <<< Leaving <0x7a83b010> -[ViewController tableView:numberOfRowsInSection:] >>>
    2014-11-14 22:02:19.488 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.491 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.493 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.494 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.496 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.497 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"
    2014-11-14 22:02:19.498 OK[2611:140791] Could not load the """" image referenced from a nib in the bundle with identifier "com.ashrafmustafa.OK"

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    Ok, so the only logs you have are the ones I have shown you. Those are examples. You need to keep adding logs at various points so that you can identify what is happening. Just like this is logging the number of rows, now you need to track down what is happening with your image data.

    The last set of messages appears to be errors of some kind. Looks like there is a bad image name in there somewhere. That's something else you can log.

    The point of this is that you should not be here asking why you don't get an image. You should be able to find out for yourself. You should be able to see what the data is. If there is no data, then you should be able to see where the data is supposed to come from. If that part works, then you should be able to at least point to the area of code that isn't getting the data. But these are all questions that you should be able to answer yourself, and quite easily at that. Then if there is still a problem, you should be able to post here and say "here is the code I use to download this data, and here is a copy of the log output from the data that is downloaded. Here is why this is wrong, here is what it should look like, what is happening?"

    You have written (or, I'm guessing, copy-pasted) some code, and you are here assuming that your code is completely correct. Yet you are not getting the desired result. Using logs to identify values at various points will help you to determine where your code is incorrect. Once you do that, then you can ask better questions, or maybe even solve your own problem along the way.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    edited November 2014
    I appreciate your help and valuable advice BrianSlick. I will try to figure out what is wrong with the code that I took from Github but made some slight modifications to it. Thank you a bunch!
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    edited November 2014
    Alas, I solved the problem by removing this block of code:
    NSString *imageURLString;
        if (imageURLString && ![imageURLString isEqual:[NSNull null]])
        {
            NSURL *imageURL = [NSURL URLWithString:imageURLString];
            [cell.imageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@placeholder.png]];
            
            
              }
    

    and put this instead:
    [cell.imageView sd_setImageWithURL: [NSURL URLWithString: [[[myObjects objectAtIndex: indexPath.row] objectForKey: @thumbnail] objectForKey: @sqDefault]]
                       placeholderImage:[UIImage imageNamed:@placeholder.png]];
    
    Thank you BrianSlick for guiding me to find the right solution!
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    Ok, rather than simply moving on, perhaps you can explain what was wrong with your old code and why you are using this new code.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • AshtashooterAshtashooter MalaysiaPosts: 25 @
    The new code fetches the value of the "thumbnail" key for a particular section and row and then assigns it to the image.
Sign In or Register to comment.