I’m driving myself crazy trying to figure out how to implement tableView numberofRowsinSection and was hoping I could get some tips or suggestions. I feel like I’ve over-complicated the entire thing. Let me start by posting the converted xml data that is now an NSDictionary.
{
"@body" = "";
"@class_id" = "";
"@title" = Discussions;
"@type" = "Discussion Block";
},
{
"@body" = "";
"@class_id" = "";
"@col" = 1;
"@title" = YouTube;
"@type" = "Youtube Block";
item = {
"@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
"@title" = "Gavin DeGraw - Not Over You";
"@type" = link;
};
},
{
"@body" = "";
"@class_id" = "";
"@title" = Calendar;
"@type" = "Calendar Block";
},
{
"@body" = "<p> homework test</p> ";
"@class_id" = "";
"@title" = Homework;
"@type" = "File Block";
item = (
{
"@body" = "https://MYDOMAIN.com/securelink;
"@title" = "crashreport.rtf";
"@type" = file;
},
{
"@body" = "mydomain.com/securelink";
"@title" = "Chem notes sep 26.docx";
"@type" = file;
}
);
},
{
"@body" = "<p> Link testing</p> ";
"@class_id" = "";
"@title" = Links;
"@type" = "Link Block";
item = (
{
"@body" = "http://google.com";
"@title" = Link1;
"@type" = link;
},
{
"@body" = "http://yahoo.com";
"@title" = link2;
"@type" = link;
},
{
"@body" = "http://reddit.com";
"@title" = link3;
"@type" = link;
}
);
}
)
The above data is the response. The goal for this is to have the first title key be the table headers, and for the second “title” key (inside “item”) to be the rows for their respective sections. In the tableView TitleforHeaderinSection method I simply use this code
for (NSDictionary *roster in response) {
[blockName addObject:[roster objectForKey@"@title"]];
}
return [blockName objectAtIndex:section];
The numberofRowsinSection method is where things get tricky. This is what I tried
for (NSDictionary *myDict in [[response objectAtIndex:section] valueForKeyPath:@"item"]) {
NSArray *keys = [myDict allKeys];
if ([keys containsObject:@"@title"]) {
array3 = [[NSMutableArray alloc] init];
[array3 addObject:[myDict objectForKey:@"@title"]];
}
}
return array3;
The error that I’m getting is that myDict is an NSString and allKeys can’t be used on it. I’m not sure why this is happening because when I change it to objectAtIndex:1 and log it the allKeys method works. I’m also pretty sure this isn’t the best way to find the number of rows needed in each section, so if there is a better way please share.
Just to clarify, this is what I’m looking for in my tableView after parsing the above data:
Header (Discussions)
-No rows under this header
Header (YouTube)
-Row (Gavin DeGraw - Not Over You)
Header (Calendar)
-No rows
Header (Homework)
-Row (crashreport)
-Row (chem notes)
Header (links)
-Row (link1)
-Row (link2)
-Row (link3)
Thanks for your help. I’ve done so much research on NSDictionary and NSArray methods, but I’m not sure how to fix this problem or code this any other way.
The problem is here:
This is fine for the dictionary below, because it loops through an NSArray of NSDictionary objects. So when
[myDict allKeys]is called, you are actually calling it on an NSDictionary.But it is not fine for the next dictionary because you are looping through a dictionary instead of an array, so the for..in loop is looping the keys of the dictionary. Then, you are calling
allKeyson an NSString (aka on a key in the dictionary).You could do something like this instead: