Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Can't get tablecellview to actually display the content I want

Hello.

As you may understand from my question I am super new to ios developing (actually to any kind of coding), but I was doing pretty good in the ios development track that I have opted for a quick jump into my app idea development.

Here is my problem: Thanks to Amit Bijlani I have been to devel a test database on a website that I have, and I have been able to log the content of the database on my console.

Now I am stuck as I can't get my app show the content of this page in ma tab view and it is killing me.

So, because I have been able to log the json data to the console, I guess the problem must be in the code I have put into the table view data source, and here is my code there:

"#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 self.gamesDictionary.count; }

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

    // Configure the cell... NSString *object = self.gamesDictionary[indexPath.row]; cell.textLabel.text = object;

    return cell; }"

I still get the console to show the right thing, but the app is still blank (I only see the white tabs).

Any ideas about where I am going wrong?

Just for complete info I am pasting my viewDidLoad code too:

"[super viewDidLoad];

NSURL *gamesMainURL = [NSURL URLWithString:@"http://www.vittoriosomaschini.com/betgame/service.php"];

NSData *gamesList = [NSData dataWithContentsOfURL:gamesMainURL];


NSError *error = nil;

NSMutableDictionary *datas = [NSJSONSerialization JSONObjectWithData:gamesList options:0 error:&error];

NSMutableDictionary *gamesDictionary = [datas valueForKey:@"Name"];

NSLog(@"%@", gamesDictionary);"

Thank you very much

Vittorio

4 Answers

you need to store an array thats contained in the dictionary. right now you are using the dictionary itself as your datasource, and thats not going to work. Im not sure how your JSON dictionary looks, so a lot of this code is based on assumptions.

NSMutableDictionary *datas = [NSJSONSerialization JSONObjectWithData:gamesList options:0 error:&error];

NSMutableDictionary *gamesDictionary = [datas valueForKey:@"Name"];

NSLog(@"%@", gamesDictionary);

//store the array of game titles contained in the dictionary
self.gamesArray = [gamesDictionary objectForKey:@"games"];

then in your delegate methods use the gamesArray property

- (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 self.gamesArray.count;
}


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

    // Configure the cell...
    NSString *object = self.gamesArray[indexPath.row];
    cell.textLabel.text = object;

    return cell;
}

Can you upload your code to github?

Ok ty both.

Thanks in particular to Sean: my code line "NSMutableDictionary *gamesDictionary = [datas valueForKey:@"Name"];" was actually a try at creating what you suggested, but it needed to be an array.

The only weird this is that it did not work with objectForKey as you suggested, but it worked using valueForKey instead.

I don't understand why for now but then my code looks like this:

"[super viewDidLoad];

NSURL *gamesMainURL = [NSURL URLWithString:@"http://www.vittoriosomaschini.com/betgame/service.php"];

NSData *gamesList = [NSData dataWithContentsOfURL:gamesMainURL];


NSError *error = nil;

NSMutableDictionary *datas = [NSJSONSerialization JSONObjectWithData:gamesList options:0 error:&error];


self.gamesArray = [datas valueForKey:@"Name"];"

For the method I have simply followed the changes you had made and implementing the new property.

ty

PS

@vladimir Cezar I will sign up with Github now! I am supernew to coding! ;)

hmm strange. I would have thought objectForKey would have worked fine. maybe you had the key string wrong

Has this question been solved?