Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-19-2011, 06:13 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Location: Zaragoza
Posts: 5
iShanty is on a distinguished road
Default Sectioned UITableView with JSON Array

Hi guys,
this is my first post, so... first of all, I introduce myself. I'm programming Objective-C for iPhone SDK since one month, so.... take a little patience...

I have one problem tryin' to section a tableview.

I have a JSON string with a format like this:
Code:
{
"Clients": [
"Section": "A",
"Clients": [
{
"Id": 136,
"Referencia": "CL0146",
"RazonSocial": "AIR-FAL, S.L.",
"CIF": "B50110253",
"Provincia": "Zaragoza",
"Localidad": "VILLANUEVA DE GALLEGO"
},
{
"Id": 54,
"Referencia": "CL0051",
"RazonSocial": "AKI BRICOLAJE ESPAÑA S.L.",
"CIF": "B83985713",
"Provincia": "Barcelona",
"Localidad": "BARBERÀ DEL VALLÈS"
},
.
.
.
.
.
Of course, I have a MutableArray for every Client and every section filled in ViewDidLoad. I think everything are worked but numberOfRowsInSection.

I mean, I hardcode numberOfRowsInSection returning 1 if section == 0, and tableview shows first section well, so I think 2 arrays are well formed.

Now, the problem is that implementation of numberOfRowsInSection is
Code:
return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
And always return 0.

I don't know what I'm doing wrong, any suggest please.

Thanks.
iShanty is offline   Reply With Quote
Old 07-19-2011, 06:39 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I realize that packing a whole lot of stuff into a single line of code seems cool, but it makes reading and troubleshooting your code very difficult. So the first thing you should do is split things into multiple lines, like so:

Code:
NSArray *allKeys = [[self sections] allKeys];
NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:section];
NSArray *sectionContents = [[self sections] objectForKey:key];
NSInteger numberOfRows = [sectionContents count];
Sure, it is more lines of code. So what? This is much more readable, much easier to understand, and now you have more logging opportunities:

Code:
NSArray *allKeys = [[self sections] allKeys];
NSLog(@"allKeys is: %@", [allKeys description]);
NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"sortedKeys is: %@", [sortedKeys description]);
NSString *key = [sortedKeys objectAtIndex:section];
NSLog(@"key is: %@", key);
NSArray *sectionContents = [[self sections] objectForKey:key];
NSLog(@"sectionContents is: %@", [sectionContents description]);
NSInteger numberOfRows = [sectionContents count];
NSLog(@"numberOfRows is: %d", numberOfRows)
So now you can ask questions of your own code and figure out where things are going wrong.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-19-2011, 07:18 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Location: Zaragoza
Posts: 5
iShanty is on a distinguished road
Default

Ok, you're right. Doin' it by your way I found that the problem is

Quote:
NSArray *sectionContents = [self.sections objectForKey:key];
In ViewDidLoad, I filled sections by that way

Quote:
//c is the name of the section (only one char)
[self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
So, sections has data correctly, but I don't understand why when I ask for the object for 'A' key for example, the result is an empty array.
iShanty is offline   Reply With Quote
Old 07-19-2011, 08:19 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
[self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
1. All this does is create an array. The array doesn't have any contents.

2. The array is leaked.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-20-2011, 07:39 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Location: Zaragoza
Posts: 5
iShanty is on a distinguished road
Default

I'm vey confused.
BrianSlick, you're right again. Now I have other problems... I think that I choose a wrong way to construct JSON string for a sectioned tableview.

I think the best way is include Section node as subnode os every client, so I have only one array in the response.

What do you think??

Thank you very much.
iShanty is offline   Reply With Quote
Reply

Bookmarks

Tags
json, sectioned, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 413
15 members and 398 guests
7twenty7, blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, Mah6447, n00b, nyoe, pungs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:56 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0