2

Xcode 的新手,很高兴能得到这个问题的答案。我正在尝试从 rss 提要中获取图像 url 标签并将其添加到我的自定义表格视图单元格中。其他文字我很好。

这是 rss 行:

<Item>
<title>this is a title</title>
<description>this is a description</description>
<link>http://www.something.com</link>
<pubDate>Fri, 09 Aug 2013</pubDate>
<enclosure type="image/jpg" url="http://www.ThisIsTheUrlIWant.com" />
</item>

这是我的代码的一部分,没有任何图像逻辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {
CustomCellNews *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];
cell.titleLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
cell.descriptionLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
//set cell image here
return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:    (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

element = elementName;

if ([element isEqualToString:@"item"]) {

    item    = [[NSMutableDictionary alloc] init];
    title   = [[NSMutableString alloc] init];
    link    = [[NSMutableString alloc] init];
    description = [[NSMutableString alloc] init];
    //image stuff
}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"item"]) {

    [item setObject:title forKey:@"title"];
    [item setObject:link forKey:@"link"];
    [item setObject:description forKey:@"description"];
    //image stuff

    [feeds addObject:[item copy]];

}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if ([element isEqualToString:@"title"]) {
    [title appendString:string];
} else if ([element isEqualToString:@"link"]) {
    [link appendString:string];
} else if ([element isEqualToString:@"description"]) {
    [ingress appendString:string];
}
//image stuff

}

谢谢

4

2 回答 2

1

首先从方法内的附件元素中的属性字典中获取图像 URL didStartElement

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//NSLog(@"Main View: didStartElement: %@\tstart", elementName);

element = elementName;

if ([element isEqualToString:@"item"]) {

    item        =       [[NSMutableDictionary alloc] init];
    title       =       [[NSMutableAttributedString alloc] init];
    description =       [[NSMutableAttributedString alloc] init];
    link        =       [[NSMutableString alloc] init];

}


if ([element isEqualToString:@"enclosure"]) {
    imageType   =   [attributeDict objectForKey:@"type"];
    imageUrl    =   [attributeDict objectForKey:@"url"];

}
//NSLog(@"RSS Utility: didStartElement: %@", elementName);

}

然后在方法内添加imageUrl到您的item数组中didEndElement

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"item"]) {

    [item setObject:title forKey:@"title"];
    [item setObject:link forKey:@"link"];
    [item setObject:description forKey:@"description"];
    [item setObject:imageType forKey:@"imageType"];
    [item setObject:imageUrl forKey:@"imageUrl"];
    [_feeds addObject:[item copy]];

}

}

这可能不适用于所有 RSS 提要。我建议您在 中放置NSLog语句didStartElement以了解可以找到图像 url 的位置:

NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
于 2014-05-06T05:22:11.437 回答
1

尝试这个:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"enclosure"]) {
        NSString *units = [attributeDict objectForKey:@"url"];
    }
}

希望这可以帮助。

于 2013-08-14T12:35:02.030 回答