這是後續行動。我已經完成了我想要做的事情。
我有一個ListViewController類和一個存儲庫模型/對象。爲了解析XML,我使用了XPathQuery,Matt Gallagher將libxml2改編爲Cocoa(http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html)。
ListViewController跟蹤當前的XML路徑。當選擇表視圖中的單元格時,相應的路徑將連接到現有路徑。 ListViewController創建並推送自己的另一個實例,PerformXMLPathQuery()返回一個數組來填充表格視圖單元格。存儲庫包含一個NSMutableData對象和一個包含當前路徑的NSString。兩者都是XPath查詢所必需的:
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);
PerformXMLPathQuery爲您做了很多後期工作。這是我用它來得到我從XML想:
-(NSArray*)getListFromRepositoryWithPath:(NSString *)path {
// Get the nodes referred to by the path
NSArray *nodes = PerformXMLXPathQuery(repository.data, path);
// Build an array with the node names
NSMutableArray* sublist = [[NSMutableArray alloc] init];
for (NSDictionary* node in nodes) {
for (id key in node) {
if ([key isEqualToString:@"nodeChildArray"]) {
NSArray* subNodes = [node objectForKey:@"nodeChildArray"];
for (NSDictionary* subNode in subNodes) {
for (id subKey in node) {
if ([subKey isEqualToString:@"nodeName"]) {
[sublist addObject:[subNode objectForKey:subKey]];
}
}
}
}
}
// Ignore duplicate entries in the data
if ([sublist count] > 0) {
return [NSArray arrayWithArray:sublist];
}
}
return [NSArray arrayWithArray:sublist];
}
當選擇一排,我用didSelectRowAtIndexPath方法準備下一個路徑和視圖控制器:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *nextXpathQueryString = [self.xpathQueryString copy];
// Build the path string for the next view's XML path query
if ([[nextXpathQueryString lastPathComponent] isEqualToString:@"*"]) {
nextXpathQueryString = [nextXpathQueryString stringByDeletingLastPathComponent];
nextXpathQueryString = [nextXpathQueryString stringByAppendingPathComponent:@ROOTNAME];
}
nextXpathQueryString = [nextXpathQueryString stringByAppendingPathComponent:[repository.currentListToDisplay objectAtIndex:indexPath.row]];
// Navigation logic. Create and push another view controller.
ListViewController *detail = [[ListViewController alloc] init];
// Populate the new ViewController
[detail setRepository:repository];
[detail setTitle:nextXpathQueryString];
[detail setXpathQueryString:nextXpathQueryString];
[nextXpathQueryString release];
UIBarButtonItem *doneButton = [[self navigationItem] rightBarButtonItem];
[[detail navigationItem] setRightBarButtonItem:doneButton animated:YES];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
很酷的事情是ListViewController的每個實例保留自己的路徑字符串和內容數組。沒有混搭嵌套的樹對象或指針。
基本上我有一個靈活的XML樹瀏覽器的iPhone。感謝堆棧溢出。
新的詞彙:鑽取http://www.iphonedevsdk.com/forum/iphone-sdk-development/53605-dynamic-navigation-xml-tables.html 我看到如何可以序列化表的數組/ unarchived,但那些意見呢? – slater 2010-10-11 14:27:54