1
votes

I have a simple JSON feed which I can parse and see in the console but I can't seem to get it to display in my table view. I am using Xcode 5.0.2. I am not sure why I can see it in the for loop when echoed to the console, but not in the table view.

FYI, I am brand new to building in xcode and objective c and I am using this video tutorial as guide.

My Json looks like this:

[
    {
        "first_name": "Bill"
    },
    {
        "first_name": "Ted"
    },
    {
        "first_name": "George"
    } etc...
]

PhotoTableViewController.h

#import 
#import "DisplayViewController.h"

@interface PhotosTableViewController : UITableViewController {

    IBOutlet UITableView *mainTableView;

   NSArray *news;
   NSMutableData *data;

}

@end

PhotoTableViewController.m


@interface PhotosTableViewController () {
    NSMutableArray *photos;
    NSArray *_locations;

}

   - (void)viewDidLoad
{

    NSURL *url = [NSURL URLWithString:@"http://feedurl.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

// some code left out for brevity


- (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 photos.count;
    NSLog(@"news count: %lu", news.count);

    return news.count;
}


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

    cell.textLabel.text = [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"];
    NSLog(@"anything: %@", [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"] );


    return cell;

}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    news = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSDictionary *first_name;

   for (int i=0; i (cant put less than sign here breaks code view) [news count] i++)
    {
        first_name = [news objectAtIndex:i];
        NSLog(@"Statuses: %@", [first_name objectForKey:@"first_name"]);
   }


    [mainTableView reloadData];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to the internet." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];

    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

1
From a first glance, your code looks just fine. You say you can see the NSLog output in the console - can you also see this one: news count: N?plu
No the news count is 0, I can see the count inside connectionDidFinishLoading area but nowhere else, it does get the count there.Brian Barthold
Did you tell the mainTableView who is the delegate and datasource?plu
I believe so, but is there a way I can verify this? Is that in the pPhotoTableViewController.h file?Brian Barthold
Usually you should find a line like mainTableView.dataSource = ... if it is done in code. Or you should find it in interface builder, in the Outlets of the table view.plu

1 Answers

0
votes

Your code looks OK by inspection. You don't seem to be using a UITableViewController subclass since you have the tableview named so you have to set the dataSource and delegate properties either in Interface Builder or in code. In IB you right-click the tableView object and there are delegate and dataSource outlets at the top of the connections listing. Drag from the circle to the viewController for each. In code, you would do, in the .m file:

// Claim that your view controller conforms to the tableview protocols
@interface YourViewControllerClass () <UITableViewDataSource, UITableViewDelgate>
...
// somewhere early on, like in viewDidLoad or viewDidAppear or other initialization code
mainTableView.delegate = self;
mainTableView.dataSource = self;

When I have these kinds of problems I tend to sprinkle NSLogs in the tableView methods to see that they are being called (or not).