我在尝试解析XML文件并在UITableView上显示由对象属性分组的结果时遇到了一些麻烦。 这是XML文件,如果有必要,我可以更改结构:
<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>
此时我有一个名为servs的NSMutableArray包含对象(Anuncio)。
这是结构:
@interface Anuncio : NSObject
NSInteger iden;
NSString *localeName;
NSString *address;
NSString *type;
所以,我想用anuncio.type用正确的titleForHeaderInSection对UITableView进行排序。
我从Apple文档下载了TableViewSuite,第二个例子,SimpleSectionedTableView似乎做了我想要的,但是它不是从XML文件中获取数据,而是获取调用[NSTimeZone knownTimeZoneNames]的数据,所以结构是完全不同,我试图使示例代码适应我的项目,但我开始意识到这似乎不是最好的方法。所以我有点卡住了。
我在考虑在主数组中包含编辑XML的对象的数组,如:
servs: {
arrayA : { anuncio1, anuncio3 }
arrayB : { anuncio2 }
}
但后来我不知道如何完成tableView方法。
这是XMLParse代码:
- (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;
}
我当前的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;
}
我得到的那一刻:
A
--------
name1
--------
A
--------
name3
--------
B
--------
name2
我希望得到:
A
--------
name1
name3
--------
B
--------
name2
我不知道我提议的模型是正确的还是我遗漏了什么。我认为这是非常普遍的事情,但我没有发现任何相关信息。 谢谢你的帮助
答案 0 :(得分:0)
在Anuncio
课程中,添加compare:
方法,如下所示:
- (NSComparisonResult) compare:(Anuncio *)otherObject {
return [self.type compare:otherObject.type];
}
然后用{:}对您的NSMutableArray
进行排序:
anuncios = (NSMutableArray *)[anuncios sortedArrayUsingSelector:@selector(compare:)];