在UITableViewCell中显示来自RSS feed的图像

时间:2015-02-11 04:39:56

标签: ios objective-c uitableview rss

我有一个带有以下示例项

的RSS源
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title/>
<atom:link href="http://www.petefreeman.com/feed/" rel="self" type="application/rss+xml"/>
<link>http://www.petefreeman.com</link>
<description/>
<lastBuildDate>Wed, 21 Jan 2015 20:34:04 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=4.1</generator>
<item>
<title>The Greatest Advice I’ve Ever Been Given</title>
<link>
http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/
</link>
<comments>
http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/#comments
</comments>
<pubDate>Mon, 11 Aug 2014 03:06:16 +0000</pubDate>
<dc:creator>
<![CDATA[ petefreeman14 ]]>
</dc:creator>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<guid isPermaLink="false">http://www.petefreeman.com/?p=161</guid>
<description>
<![CDATA[
<img src="http://i2.wp.com/www.petefreeman.com/wp-content/uploads/2014/08/rabbit.jpg?fit=1024%2C1024" class="attachment-large wp-post-image" alt="rabbit" />
 <div>
 <a href="http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/"><img title="rabbit" src="http://i2.wp.com/www.petefreeman.com/wp-content/uploads/2014/08/rabbit.jpg?fit=200%2C200" alt="&lt;a href=&quot;http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/&quot; title=&quot;Permalink to The Greatest Advice I&#039;ve Ever Been Given&quot; rel=&quot;bookmark&quot;&gt;&lt;strong&gt;The Greatest Advice I&#8217;ve Ever Been Given&lt;/strong&gt;&lt;/a&gt;" width="" /></a>
 </div>
 *Warning: The following conversation between a fictional man and a fictional rabbit changed my life. Reader discretion is advised. &#160; &#160; Here&#8217;s how it played out. Brer Rabbit was pouting about what he had and what he didn&#8217;t have, and how he never seemed to get what he wanted. Uncle Remus wisely acknowledged Brer Rabbit [&#8230;]
]]>
</description>
<content:encoded>
<![CDATA[
<img src="http://i2.wp.com/www.petefreeman.com/wp-content/uploads/2014/08/rabbit.jpg?fit=1024%2C1024" class="attachment-large wp-post-image" alt="rabbit" />
 <div>
 <a href="http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/"><img title="rabbit" src="http://i2.wp.com/www.petefreeman.com/wp-content/uploads/2014/08/rabbit.jpg?fit=200%2C200" alt="&lt;a href=&quot;http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/&quot; title=&quot;Permalink to The Greatest Advice I&#039;ve Ever Been Given&quot; rel=&quot;bookmark&quot;&gt;&lt;strong&gt;The Greatest Advice I&#8217;ve Ever Been Given&lt;/strong&gt;&lt;/a&gt;" width="" /></a>
 </div>
 <p>*Warning: The following conversation between a fictional man and a fictional rabbit changed my life. Reader discretion is advised.</p> <p>&nbsp;</p> <p></p> <p>&nbsp;</p> <p>Here&#8217;s how it played out. Brer Rabbit was pouting about what he had and what he didn&#8217;t have, and how he never seemed to get what he wanted. Uncle Remus wisely acknowledged Brer Rabbit and said the following life-changing words &#8211; <em>&#8220;<strong>It&#8217;s not what you got. It&#8217;s what you do with what you got that counts.&#8221;</strong></em></p> <p>&nbsp;</p> <p>These 15 words changed my life. Seriously. Think about all the possessions you <del datetime="2014-08-11T02:24:50+00:00">have</del> think you have to have before you can make a change in the world. Before you can start your business. Before you are secure enough to start a family. That number is <strong>overwhelming</strong>.</p> <p>Will we ever get enough? No. And neither will Brer Rabbit. But there is a <em>second path</em>, as Uncle Remus points out. And Remus was right. When Brer Rabbit stopped taking and started making, his world turned upside down.</p> <p>We all tend to look around at our houses, cars and bank accounts and think, &#8220;what will it take for me to get x new appliance/y new luxury car/z amount of dollars.&#8221; Inevitably, our solution is to get the resources to get the new x, y or z. <strong>But this is backwards!</strong> Just like Brer Rabbit, we already have the resources we need to move forward with our lives. It&#8217;s not <em>what</em> resources we&#8217;ve got, it&#8217;s <em>what we do</em> with those resources that will affect our outcome.</p> <p>After hearing this conversation between Remus &amp; Rabbit (shoutout Grandpa for making this decades-old cartoon come to life. I love you.), I took inventory. Inventory of my bedroom, inventory of my car, inventory of my bank account. I sat down with a spreadsheet of my resources and a word document of my goals. After many connecting lines later, I had mapped out what I was going to do with what I had to get what I want. The following year, I co-founded and became CEO of a nonprofit business that paired disadvantaged elementary school students with high school mentors. And looking back? I didn&#8217;t need more influence, more money, more <em>resources</em>. All I needed was my co-founder and the guts to organize one of the most effective nonprofits our high school had ever seen.</p> <p>Lack of material want and resourcefulness are two very lucky traits I have, and they’re something that I implore more of you to focus on.</p> <hr /> <p style="text-align: center;"><em><strong>If you found value in this article, it would mean a lot to me if you tweeted @mrpetefreeman &amp; told me!</strong></em></p> <hr /> <p>I&#8217;m an innovator. You can find out more at <a href="http://www.petefreeman.com/about">wwww.petefreeman.com/about</a></p>
]]>
</content:encoded>
<wfw:commentRss>
http://www.petefreeman.com/the-greatest-advice-ive-ever-been-given/feed/
</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>

到目前为止,我已成功取消了标题和链接!但是图像有点棘手。我的主要目标是将图像链接变为我的代码中的变量,我想我可以弄清楚如何从那里显示它,但它很奇怪。它包含在&#34;内容中:编码&#34;阻止和&#34; [CDATA]&#34;阻止,我不知道如何成功访问它,它肯定不像其他人一样工作。我试过&#34;内容:编码&#34;这给了我太多的方式,其他人返回零。这是我的代码

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

    //NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
    element = elementName;

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

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


- (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:image forKey:@"content:encoded"];

        [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:@"content:encoded"]) {
        [image appendString:string];
         NSLog(image);
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

如果有人能提供帮助那就太棒了,谢谢!

1 个答案:

答案 0 :(得分:0)

我认为当你阅读内容时:编码它有CDATA标签中的所有html?由于该内容不是结构化XML,因此您需要自己解析该HTML以从中获取img。

解析XML并解析内容:编码部分中的HTML