简体   繁体   中英

I can't get my array from JSON to load into the UITableView

The .h contains

@interface HomeTVC : UITableViewController
@property (nonatomic, strong) NSArray *spotTitle;

and the .m contains

#import "HomeTVC.h"

@interface HomeTVC ()

@end

@implementation HomeTVC

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

This is where I make the server request and get the data

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
    NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
    NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
    NSArray *spotTitle = [spotTableDictionary valueForKey:@"title"];
    NSLog(@"%@",spotTitle);

That NSLog prints the array I want to pass into the tableview in the terminal and everything here seems to work properly.

Here's where my problems start:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.spotTitle count];
}

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

    cell.textLabel.text = [self.spotTitle objectAtIndex:indexPath.row];
    return cell;
}

I've been fumbling around trying to connect the array that gets passed to that NSLog statement to my tableview and I'm not sure what I'm doing wrong at this point.

Both dataSource and delegate are connected to HomeTVC in the main storyboard.

The program compiles and runs with no errors or warnings and loads an empty tableview.

Maybe I'm missing something super basic (I'm really new to programming) and this is the first time I've tried to pass in an external db.

NSArray *spotTitle = [spotTableDictionary valueForKey:@"title"];
self.spotTitle = spotTitle; //Do you forget assign to your property?
NSLog(@"%@",self.spotTitl);

Why to create another variable when you can use your property. Just do this::

- (void)viewDidLoad
{
    [super viewDidLoad];

NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
self.spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",self.spotTitle);

That should solve your problem. Also do not use same name for property & local variables, it leads to unnecessary confusion as you are dealing with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM