2015-02-11 49 views
0

我有下面的示例項目Displaing從RSS源圖像的UITableViewCell

<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> 

到目前爲止,我已經成功地拉了標題和鏈接的RSS源!但圖像有點棘手。我的主要目標是將圖像鏈接變成我的代碼中的一個變量,我想我可以弄清楚如何從那裏顯示它,但它的奇怪。它被封裝在一個「content:encoded」塊和一個「[CDATA]」塊中,我不確定如何成功訪問它,它肯定不像其他的那樣工作。我試過「內容:編碼」,這給我太多的方式,其他人返回零。這裏是我的代碼

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

} 

如果有人可以幫助這將是真棒,謝謝!

回答

0

我會假設,當你閱讀的內容:編碼,它有所有的CDATA標籤中的HTML?由於該內容不是結構化的XML,因此您需要自己解析該HTML以從中獲取img。

解析XML並解析HTML中的內容:編碼部分