问题描述
Xcode的新手,很高兴得到这个问题的答案。我试图从rss feed获取图像url标记并将其添加到我的自定义表格视图单元格。其他文字我没事。
Newbie to Xcode and would be thrilled to get an answer to this question. I am trying to get the image url tag from a rss feed and add it to my custom table view cell. The other text I get fine.
这是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>
这是我的代码的一部分,没有任何图像逻辑:
This is part of my code without any image logic:
- (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
}
谢谢
推荐答案
首先从 didStartElement 方法:
First get the image URL from the attributes dictionary in the enclosure element inside the didStartElement
method:
- (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
到 didEndElement
方法中的 item
数组:
Then add imageUrl
to your item
array inside the didEndElement
method:
- (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 feed。我建议您在 didStartElement
中放置 NSLog
语句,以了解可以找到图像网址的位置:
This may not work for all rss feeds. I recommend you put NSLog
statements inside the didStartElement
to understand where the image url can be found:
NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
这篇关于Xcode从rss feed获取图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!