- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试构建一个显示来自 http://news.yahoo.com/rss/ 的最新 RSS 提要的应用程序我能够使用 NSXMLParserDelegate 解析 xml,我能够显示日期和时间以及特定提要的标题,但我还想为每个提要显示图像,我知道如何解析主要元素但我不知道如何传递以 url 作为键的子元素的属性......例如上面的提要包括以下内容:
<item>
<title>fooo</title>
<description ahref="some link" image src="http://l1.yimg.com/bt/api/res/1.2/Wo3_apH.kz7DMvOj7MDtRQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/20c464ff99815420210f6a706700a792.jpg"
</description>
我的表格 View 中需要来自 IMG SRC 的图像以及该特定提要的标题和日期。我很困惑如何解析 IMG SRC 属性并将其添加到我的 TableView 中并更改大小,以便它不会破坏我的标题和日期。
到目前为止我做了什么:
-(void)viewDidAppear:(BOOL)animated
{
if ([stories count] == 0)
{
NSString * path = @"http://news.yahoo.com/rss/";
[self parseXMLFileAtURL:path];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 80);
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"pubDate"];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[newsTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [stories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.textLabel.text=[[stories objectAtIndex: storyIndex] objectForKey: @"title"];
cell.detailTextLabel.text=[[stories objectAtIndex:storyIndex] objectForKey:@"pubDate"];
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
return cell;
}
如何获取单元格右侧的图像?
最佳答案
在回答标题中的问题时,要加载图像,您可以使用 NSXMLParser
对象解析 RSS 提要以获取字符串的 URL,以及何时向用户显示该图像,您使用该 URL 将图像检索到 NSData
中,为此创建一个 UIImage
,然后设置 的
相应。请参阅下面 list 中的 image
属性>UIImageViewcellForRowAtIndexPath
。
就实现细节而言,我已根据您正在阅读来自 http://news.yahoo.com/rss/ 的提要这一事实对这个答案进行了重大修改。它的格式与您在示例 XML 列表中显示的格式明显不同。格式实际上看起来是:
<item>
<title>Palestinian death toll ...</title>
<description>this contains the html version of the story description</description>
<link>http://news.yahoo.com/palestinian-death-toll-...</link>
<pubDate>Mon, 19 Nov 2012 11:20:15 -0500</pubDate>
<source url="http://www.reuters.com/">Reuters</source>
<guid isPermaLink="false">palestinian-death-toll-...</guid>
<media:content url="http://the.url.for.image/images/palestinian.JPG" type="image/jpeg" width="130" height="86"></media:content>
<media:text type="html">another html version of the text</media:text>
<media:credit role="publishing company"></media:credit>
</item>
其中一些只是标准的 XML 标签 title
、description
等,您可以在其中获取开始和结束标签之间的值(您可以使用NSXMLParserDelegate
方法 parser:foundCharacters:
)。但图像 URL 实际上位于 media:content
标记本身的 url
属性内,在这种情况下,您必须使用 NSXMLParserDelegate
方法didStartElement
并查询 attributes
参数。
因此,鉴于此,我上传了一个 sample Yahoo News reader它演示了解析 Yahoo RSS 提要的过程。请注意,这是一个相当原始的实现,但希望这能为您提供基本概念。
关于objective-c - 如何显示来自 RSS 提要的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457215/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
当我将 JSON 提要(Wordpress JSON 提要)解析到我的 React Native 提要页面时,我在模拟器中没有看到任何结果,以下是我正在使用的代码; ------------AppBo
我有一个网页,其中有一个搜索页面。我提供了一个用于搜索的“动态”RSS 提要,以便用户可以订阅他喜欢的任何搜索词的搜索结果。 所以我想知道如果该搜索词返回 0 个结果(这意味着我没有“项目”可放入提要
我想要测试我创建的 RSS Feed,我正在寻找一些好的 RSS Feed 应用程序来测试。 最佳答案 不要只是尝试一堆,看看它是否有效 - 验证它。让我为您谷歌一下: W3C RSS validat
我有一些 PHP 代码可以合并两个 RSS 提要。我试图按日期对提要进行排序,但我得到了一个有趣的结果: 两个 Feed 分别排序(第一个 Feed 先列出,然后第二个饲料) 第一个提要按升序排序,第
我最近开始使用“新 Google 表格”(电子表格),他们将 URL 更改为共享的公共(public)电子表格,我不确定如何获取电子表格数据的 JSON 提要。 基于来自此 URL 的数据:https
我想知道在您的应用程序中使用其他人的 RSS 提要(例如 BBC RSS 提要)是否存在任何法律问题? 最佳答案 你真的应该问律师。但是,我在 out-law.com 上找到了这个: Using a
我们有一个提供一些 RSS 提要的站点,我们想知道有多少人订阅了每个提要,而不使用像 FeedBurner 这样的系统来为他们提供服务。 解决这个问题的原始方法基本上是记录请求,然后获取请求每个提要的
我有一个系统可以获取几百个 RSS 提要。目前,它们的刷新周期为 10 分钟,但我最好让它更快。以近实时/推送间隔获取 RSS 源的策略是什么? 我遇到的一些解决方案: 1分钟取一次;如果没有变化,则
我已经开始开发一个网页,最近聘请了某人编写代码以在 http://farmball.com/ 的中间面板中显示自定义提要(由 API 提供支持)。 . 请注意,这不是与站点博客 相关联的 RSS 提要
有谁知道我在哪里可以找到这个页面的 json 提要? http://twitter.com/#!/microsoft 我找到的最接近的是这个: http://twitter.com/status/us
如何使用 nuget 命令禁用 SSL 证书检查? PS C:\Softwares> .\nuget.exe list Unable to load the service index forsour
我订阅了许多 RSS 提要,主要来自我自己的时区(英国:目前是 GMT+1,又名 BST)。不过我也对新西兰的新闻感兴趣(目前为 GMT+12)。 我的问题是由于我沉迷于需要将未读计数保持在或接近于零
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
我在 vb.net 设置中使用 fullCalendar。我使用包含在 aspx 页面中的 JSON 提要将数据传递到日历,如下所示: events: "JSONcalendarFeed.aspx"
我的应用程序使用雅虎的天气信息 (XML) 来显示 future 5 天的天气预报。当邮政编码在美国时,这很有效。例如,下面的 url 为我提供了密歇根州富兰克林的提要。 http://xml.wea
我有一个显示我的 Instagram 动态的网站。以前我在用Instagram 遵循 API。用户/ self /媒体/最近 此 API 使用我生成一次的访问 token ,并在我的代码中作为变量保存
是否可以为特定关键字获取 BlogSpot 的 RSS 提要? 我已尝试使用以下 URL,但它们似乎不起作用。 Atom 1.0: https://blogname.blogspot.com/
Basecamp 对其 RSS 提要使用 HTTP 身份验证,但这意味着 Google Reader, Bloglines and Firefox/Safari RSS don't work . 是否
是否可以创建多语言的 RSS (2.0) 提要?假设我主要用英语( en )写博客,但有时我会创建德语( de )帖子。 RSS 规范中是否对此提供支持?我在 RSS spec 中找不到任何内容在这个
我是一名优秀的程序员,十分优秀!