i’m having some troubles trying to parse an XML file and display the results grouped by an object property on a UITableView.
This is the XML file, I can change the structure if it’s necessary:
<Anuncios>
<Anuncio id="1">
<localeName>name1</localeName>
<address>address1</address>
<type>A</type>
</Anuncio>
<Anuncio id="2">
<localeName>name2</localeName>
<address>address2</address>
<type>B</type>
</Anuncio>
<Anuncio id="3">
<localeName>name3</localeName>
<address>address3</address>
<type>A</type>
</Anuncio>
</Anuncios>
At this moment I have a NSMutableArray called servs containing objects (Anuncio).
This is the structure:
@interface Anuncio : NSObject
NSInteger iden;
NSString *localeName;
NSString *address;
NSString *type;
So, I would like to sort the UITableView by anuncio.type with the correct titleForHeaderInSection.
I downloaded the TableViewSuite from the Apple documentation, and the second example, SimpleSectionedTableView seems to do what I want, but instead of getting the data from an XML file, it gets the data calling [NSTimeZone knownTimeZoneNames], so the structure is quite different, I tried to adapt the sample code to my project, but i’m starting to realize that seems to be not the best way to do it. So I’m a bit stuck.
I was thinking on having arrays inside the main array containing the objects editing the XML, like that:
servs: {
arrayA : { anuncio1, anuncio3 }
arrayB : { anuncio2 }
}
but then I don’t know how to complete the tableView methods.
Here it is the XMLParse code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Anuncios"]) {
//Initialize the array.
appDelegate.servs = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Anuncio"]) {
//Initialize the anuncio.
serv = [[Anuncio alloc] init];
//Extract the attribute here.
serv.iden = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value: %i", serv.iden);
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Anuncios"])
return;
if([elementName isEqualToString:@"Anuncio"]) {
[appDelegate.servs addObject:serv];
[serv release];
serv = nil;
}
else
[serv setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
Part of my current UITableViewController:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [appDelegate.servs count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
anuncios = (NSMutableArray *)[appDelegate.servs sortedArrayUsingSelector:@selector(compare:)];
Anuncio *anuncio = [anuncios objectAtIndex:section];
return [anuncio type];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
anuncios = (NSMutableArray *)[appDelegate.servs sortedArrayUsingSelector:@selector(compare:)];
Anuncio *anuncio = [anuncios objectAtIndex:indexPath.section];
cell.textLabel.text = anuncio.localeName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
At the moment I get:
A
--------
name1
--------
A
--------
name3
--------
B
--------
name2
and I would like to get:
A
--------
name1
name3
--------
B
--------
name2
I don’t know if the model that I’m proposing is the right one or i’m missing something. I thought it was something very common but I didn’t find anything about this.
Thanks for helping
In your
Anuncioclass, add acompare:method like this:Then sort your
NSMutableArraywith this: