我目前正在使用RaptureXML从表格视图中的url tio显示中提取数据。我设法抓住了我需要的每个字符串并将其添加到我的数组中,如下所示:
- (void)loadURL {
RXMLElement *rootXML = [RXMLElement elementFromURL:[NSURL URLWithString:@"http://api.somexml.com/xml"]];
NSLog(@"%@ %@", rootXML.tag, [rootXML attribute:@"totalEntries"]);
[rootXML iterateWithRootXPath:@"//event" usingBlock:^(RXMLElement *event) {
// NSLog(@"Event - URI: %@", [event attribute:@"uri"]);
// NSLog(@"Event - Venue: %@", [event attribute:@"displayName"]);
// NSLog(@"Event - Type: %@", [event attribute:@"type"]);
[rootXML iterateWithRootXPath:@"//location" usingBlock: ^(RXMLElement *location) {
// NSLog(@"Location - City: %@",[location attribute:@"city"]);
// NSLog(@"Location - Latitude : %@",[location attribute:@"lat"]);
// NSLog(@"Location - Longitude: %@",[location attribute:@"lng"]);
[rootXML iterateWithRootXPath:@"//start" usingBlock:^(RXMLElement *start) {
// NSLog(@"Start - Time: %@",[start attribute:@"time"]);
// NSLog(@"Start - Date: %@",[start attribute:@"date"]);
[rootXML iterateWithRootXPath:@"//performance" usingBlock:^(RXMLElement *performance) {
// NSLog(@"Performance - Artist: %@",[start attribute:@"displayName"]);
[events addObject:[NSArray arrayWithObjects:
[event attribute:@"uri"],
[event attribute:@"displayName"],
[event attribute:@"type"],
[location attribute:@"city"],
[location attribute:@"lat"],
[location attribute:@"lng"],
[start attribute:@"time"],
[start attribute:@"date"],
[performance attribute:@"displayName"],
nil]];
}];
}];
}];
}];
}
问题在于,当我指定行数时,它会返回一个大数而不是6。
return [events count];
这就是XML文件的样子:
<resultsPage totalEntries="6" perPage="50" page="1" status="ok">
<results>
<event uri="http://somexml.com/xml" popularity="0.863682" displayName="Radio 1's Hackney
Weekend 2012" id="9234656" type="Festival" status="ok">
<location city="London, UK" lng="-0.128" lat="51.5078"/>
<series displayName="Radio 1's Hackney Weekend"/>
<end time="" date="2012-06-24" datetime=""/>
<start time="" date="2012-06-23" datetime=""/>
<performance displayName="Lucy Labeaux" billingIndex="5" id="23336188" billing="headline">
<artist uri="http://www.somexml.com/artistxml" displayName="Lucy Labeaux" id="1168415">
<identifier href="http://somexml.com.xml" mbid="4593d49a-7f67-46ba-9ec0-126bd676286f"/>
</artist>
</performance>
感谢您的帮助!
**
**
所以我想出了不断运行代码的东西。如果你看下面我分别在我的数组中添加对象。
- (void)loadURL {
RXMLElement *rootXML = [RXMLElement elementFromURL:[NSURL URLWithString:@"http://somexml.com/xml"]];
NSLog(@"%@ %@", rootXML.tag, [rootXML attribute:@"totalEntries"]);
[rootXML iterateWithRootXPath:@"//event" usingBlock:^(RXMLElement *event) {
[events addObject:[NSArray arrayWithObjects:
[event attribute:@"uri"],
[event attribute:@"displayName"],
[event attribute:@"type"], nil]];
}];
[rootXML iterateWithRootXPath:@"//location" usingBlock: ^(RXMLElement *location) {
[events addObject:[NSArray arrayWithObjects:
[location attribute:@"city"],
[location attribute:@"lat"],
[location attribute:@"lng"],
nil]];
}];
[rootXML iterateWithRootXPath:@"//start" usingBlock:^(RXMLElement *start) {
[events addObject:[NSArray arrayWithObjects:
[start attribute:@"time"],
[start attribute:@"date"],
nil]];
}];
}
现在,当我调用我的对象以便将它们链接到界面时,我执行了以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
EventsCell *cell = (EventsCell *)[tableView dequeueReusableCellWithIdentifier:@"EventsCell"];
cell.venueLabel.text = [NSString stringWithFormat:@"%@",[[events objectAtIndex:indexPath.row] objectAtIndex:2]];
return cell;
}
我意识到,不是在索引处创建总共8个对象,而是仅创建3个。每次添加新对象时,它都会将其添加到这3个索引中。例如,如果我将我的场地标签分配给objectAtIndex 2,我不仅会获得6行显示类型的标签,还会显示另外6行显示时间。
这是我的viewDidLoad的样子:
- (void)viewDidLoad
{
[super viewDidLoad];
self.events = [[NSMutableArray alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadURL];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
答案 0 :(得分:0)
看到没有人回答这个问题作为“答案”,但在评论中留下了答案,我将接受这个作为答案。