XMLを適切に解析する方法を学ぶための優れたチュートリアルがたくさんあります。ここに簡単な説明があります。
.hで
@interface ObjectName : ObjectSuperclass <NSXMLParserDelegate> {
NSMutableString *currentElement;
NSMutableString *childElement;
NSMutableDictionary *dictionary;
}
@end
.mに、次のNSXMLParserデリゲートメソッドを挿入します。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = nil;
currentElement = [elementName copy];
if ([elementName isEqualToString:@"xmlParentElement"]) {
//This means the parser has entered the XML parent element named: xmlParentElement
//All of the child elements that need to be stored in the dictionary should have their own IVARs and declarations.
childElement = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//For all child elements, run this if statement.
if (currentElement isEqualToString:@"childElement") {
[childElement appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"parentElement"]) {
[dictionary addObject:childElement forKey:@"childElement"];
//And devise a system for indexing (this could be converting the address string in to an array and taking objectAtIndex:0.. any way you choose, add that object below:
[dictionary addObject:@"A" forKey@"index"];
}
}
これで、通常のUITableViewControllerデリゲートメソッドを使用してテーブルを作成し、さらに次のデリゲートメソッドを使用してサイドインデックスを作成します。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [dictionary objectForKey:@"index"];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}