[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

10/28/2009 at 09:31AM PDT, ID: 24851595 | Points: 500
[x]
Attachment Details

Help with Sections in UITableview from XML

Asked by matttrout in Objective-C Programming Language, iPhone, Apple Programming

Tags: iphone, uitableview, sections, xml, parsing, cocoa, xcode

Hey experts,

Having some trouble with parsing my XML doc into sections in my UITableView. Here's what I'm trying to do....

I have an large XML document setup in the following manner....
<Schedule>
   <Month name="October 2009">
      <Game>
         <Date>
         <HomeTeam>
         <AwayTeam>
         <Time>
         <Result>
      </Game>
   </Month>
   <Month name="November 2009">
      <Game>
         <Date>
         <HomeTeam>
         <AwayTeam>
         <Time>
         <Result>
      </Game>
   </Month>
</Schedule>
etc. etc. etc.

I am loading it into a UITableview. I am able to parse the <Month>, grab the name and load it into an array called "months" and also do the same thing for <Game>. What I want is to create a section for each month and put the games for each month in their respective sections. Using NSLog I was able to verify that 7 objects were created and added to the "months" array...but when I use return [months count]; in numberOfSectionsInTableView, the app crashes and gives runtime error of "must return at least one section", which tells me either my NSLog was lying to me or that it is not counting the array.

Kind of stuck and would appreciate any help anyone could give! My code relative to creating sections and parsing XML is below....Thanks in advance!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [months count];
}
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [games count];
}
 
 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return @"Default";
}
 
- (void)parseXMLFileAtURL:(NSString *)URL
{	
	games = [[NSMutableArray alloc] init];
	months = [[NSMutableArray alloc] init];
	
    //you must then convert the path to a proper NSURL or it won't work
    //NSURL *xmlURL = [NSURL URLWithString:URL];
	NSString *Path = [[NSBundle mainBundle]bundlePath];
	NSString *DataPath = [Path stringByAppendingPathComponent:@"Schedule.xml"];
	NSData *Data = [[NSData alloc]initWithContentsOfFile:DataPath];
	
    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithData:Data];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
	
    [rssParser parse];
	
}
 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	currentElement = [elementName copy];
	if ([elementName isEqualToString:@"Month"]) {
		// clear out our story item caches...
		item = [[NSMutableDictionary alloc] init];
		monthName = [[NSMutableString alloc] initWithString:[attributeDict valueForKey:@"name"]];
		NSLog(@"Adding A Month with name: %@", monthName);
	}
	
	else if ([elementName isEqualToString:@"Game"]) {
		// clear out our story item caches...
		item = [[NSMutableDictionary alloc] init];
		gameDate = [[NSMutableString alloc] init];
		gameTime = [[NSMutableString alloc] init];
		theHomeTeam = [[NSMutableString alloc] init];
		theVisitorTeam = [[NSMutableString alloc] init];
		theGameResults = [[NSMutableString alloc] init];
	}
	
}
 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"Month"]) {
		// save values to an item, then store that item into the array...
		[item setObject:monthName forKey:@"name"];
		
		[months addObject:[item copy]];
		NSLog(@"Adding A New Month %@", monthName);
		NSLog(@"months array has %d items", [months count]);
	}
	
	else if ([elementName isEqualToString:@"Game"]) {
		// save values to an item, then store that item into the array...
		[item setObject:gameDate forKey:@"Date"];
		[item setObject:theVisitorTeam forKey:@"Visitor"];
		[item setObject:theHomeTeam forKey:@"Home"];
		[item setObject:gameTime forKey:@"Time"];
		[item setObject:theGameResults forKey:@"Results"];
		
		[games addObject:[item copy]];
		NSLog(@"Adding Game on date: %@", gameDate);
	}
	
}
 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	//NSLog(@"found characters: %@", string);
	// save the characters for the current item...
	if ([currentElement isEqualToString:@"Month"]) 
	{
		[monthName appendString:string];
	}
	else if ([currentElement isEqualToString:@"Date"]) 
	{
		[gameDate appendString:string];
	} 
	else if ([currentElement isEqualToString:@"Visitor"]) 
	{
		[theVisitorTeam appendString:string];
	} 
	else if ([currentElement isEqualToString:@"Home"]) 
	{
		[theHomeTeam appendString:string];
	} 
	else if ([currentElement isEqualToString:@"Time"])
	{
		[gameTime appendString:string];
	} 
	else if ([currentElement isEqualToString:@"Results"]) 
	{
		[theGameResults appendString:string];
	}
	
	
}
[+][-]11/14/09 02:36 PM, ID: 25822691

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625