2

我目前正在使用 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>

谢谢您的帮助!

**

获取与我的数组对象相关的错误。

**

所以我从不断运行代码中发现了一些东西。如果您看下面,我将对象添加到我的数组中 3 次。

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

}
4

1 回答 1

0

看到没有人将这个问题作为“答案”回答,而是在评论中留下了答案,我将接受这个作为答案。

于 2012-05-16T10:35:18.747 回答