Row Count Is Returning the incorrect number using RaptureXML

允我心安 提交于 2019-12-10 22:53:09

问题


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]];                
            }];

        }];


    }];



}];

}

The problem is that when I assign the number of rows, it's returning a large number instead of 6.

 return [events count];

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>

Thank you for your help!

**

Getting Error related to my array's objects.

**

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;

}

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.

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];
    });
});

}

回答1:


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.



来源:https://stackoverflow.com/questions/10377214/row-count-is-returning-the-incorrect-number-using-rapturexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!