[英]iPhone TBXML - Loop through reponses
What's the best way to loop through this to get all the XML items out and assign them to the Cell.text as an array? 循环遍历以获取所有XML项目并将它们作为数组分配给Cell.text的最佳方法是什么?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil) {
Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
TBXML * XML = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.tomstest.info/ios/results.xml"]] retain];
TBXMLElement *rootXML = XML.rootXMLElement;
TBXMLElement *results = [TBXML childElementNamed:@"location" parentElement:rootXML];
TBXMLElement *WOEID = [TBXML childElementNamed:@"CompanyName" parentElement:results];
NSString *woeid = [TBXML textForElement:WOEID];
Cell.text = woeid;
return Cell;
}
Thanks 谢谢
Tom 汤姆
First, you really shouldn't download the content of your file in tableView:cellForRowAtIndexPath:
. 首先,您实际上不应该将文件的内容下载到tableView:cellForRowAtIndexPath:
。 The method is called once for each cell: you would end up downloading the xml file many times. 该方法为每个单元格调用一次:您最终将多次下载xml文件。
TBXML
does not support XPath queries, so you'll have to loop through the results. TBXML
不支持XPath查询,因此您必须遍历结果。 Something like 就像是
NSMutableArray *cellTitlesBuffer = [NSMutableArray array];
TBXMLElement *locationNode = [TBXML childElementNamed:@"location" parentElement:rootXML];
if (locationNode) {
NSString *cellTitle = nil;
do {
TBXMLElement *woeidNode = [TBXML childElementNamed:@"CompanyName" parentElement:locationNode];
[cellTitlesBuffer addObject:[TBXML textForElement:woeidNode]];
} while (locationNode = locationNode->nextSibling);
}
Then store the titles buffer in a class variable (say cellTitles) and in tableView:cellForRowAtIndexPath:
然后将标题缓冲区存储在类变量(例如cellTitles)和tableView:cellForRowAtIndexPath:
Cell.textLabel.text = [cellTitles objectAtIndex:indexPath.row];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.