问题描述
我目前正在使用RaptureXML从表视图内的url tio显示中提取数据.我已经设法抓住了我需要的每个字符串,并将其添加到我的数组中,如下所示:
I'm currently using RaptureXML to pull in data from a url tio display inside a table view. I've managed to grab every string I need and add it to my array, as you can see below:
- (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.
The problem is that when I assign the number of rows, it's returning a large number instead of 6.
return [events count];
这是XML文件的样子:
This is what the XML file looks like:
<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>
谢谢您的帮助!
**
**
所以我从不断运行代码中发现了一些东西.如果您在下面看,我将对象分别添加3次到数组中.
So I figured out something from constantly running the code. If you look below I add objects to my array 3 separate times.
- (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]];
}];
}
现在,当我调用对象以便将它们链接到接口时,我执行了以下操作:
Now when I call my objects so I can link them to the interface I did the following:
- (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行显示时间.
I realized that instead of creating a total of 8 objects at index, it's only creating 3. And everytime I add new objects it's adding it to those 3 indexes. For example if I assign my Venue Label to the objectAtIndex 2, I not only get 6 rows with the label displaying the type, but also another 6 displaying the time.
这是我的viewDidLoad的样子:
This is how my viewDidLoad looks:
- (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];
});
});
}
推荐答案
看到没有人以答案"的形式回答该问题,而是在评论中留下了答案,因此我将其作为答案.
Seeing that no one answered the question as an "answer" but left the answer in the comments, I'm going to just accept this as the answer.
这篇关于行计数使用RaptureXML返回不正确的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!