2013-04-22 37 views
0

我試圖在表格視圖中顯示解析的雅虎RSS提要。但屏幕上沒有顯示任何內容。當我使用斷點時,我開始知道該控件根本不會用到cellForRowAtIndexPath方法。我不知道爲什麼會這樣。雅虎飼料的結構如下:解析的飼料在UITableView上不可見,cellForRowAtIndexPath方法沒有被訪問

<?xml version="1.0" encoding="utf-8"?> 
    <rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0"> 
    <channel> 
    <title>Yahoo! News - Latest News &amp; Headlines</title> 
    <link>http://news.yahoo.com/</link> 
    <description>The latest news and headlines from Yahoo! News. Get breaking news stories and in-depth coverage with videos and photos.</description> 
    <language>en-US</language> 
    <copyright>Copyright (c) 2013 Yahoo! Inc. All rights reserved</copyright> 
    <pubDate>Sun, 21 Apr 2013 17:08:00 -0400</pubDate> 
    <ttl>5</ttl> 
    <image> 
    <title>Yahoo! News - Latest News &amp; Headlines</title> 
    <link>http://news.yahoo.com/</link> 
    <url>http://l.yimg.com/a/i/us/nws/th/main_142c.gif</url> 
    </image> 
    <item><title>After the bombings, the blame game begins</title><description>&lt;p&gt;&lt;a href="http://news.yahoo.com/boston-marathon-bombing-blame-game-begins-210800101.html"&gt;&lt;img src="http://l3.yimg.com/bt/api/res/1.2/uiv31afw5K_BzPxeYQz44Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2013-04-22T013026Z_741208741_GM1E94M0Q7101_RTRMADP_3_USA-EXPLOSIONS-BOSTON.JPG" width="130" height="86" alt="A couple embraces at a memorial on Boylston Street to the victims of the Boston Marathon bombings in Boston" align="left" title="A couple embraces at a memorial on Boylston Street to the victims of the Boston Marathon bombings in Boston" border="0" /&gt;&lt;/a&gt;Some lawmakers fault the FBI for not following up on intelligence about one of the alleged bombers.&lt;/p&gt;&lt;br clear="all"/&gt;</description><link>http://news.yahoo.com/boston-marathon-bombing-blame-game-begins-210800101.html</link><pubDate>Sun, 21 Apr 2013 17:08:00 -0400</pubDate><source url="http://www.csmonitor.com/">Christian Science Monitor</source><guid isPermaLink="false">boston-marathon-bombing-blame-game-begins-210800101</guid><media:content url="http://l3.yimg.com/bt/api/res/1.2/uiv31afw5K_BzPxeYQz44Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2013-04-22T013026Z_741208741_GM1E94M0Q7101_RTRMADP_3_USA-EXPLOSIONS-BOSTON.JPG" type="image/jpeg" width="130" height="86"></media:content><media:text type="html">&lt;p&gt;&lt;a href="http://news.yahoo.com/boston-marathon-bombing-blame-game-begins-210800101.html"&gt;&lt;img src="http://l3.yimg.com/bt/api/res/1.2/uiv31afw5K_BzPxeYQz44Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2013-04-22T013026Z_741208741_GM1E94M0Q7101_RTRMADP_3_USA-EXPLOSIONS-BOSTON.JPG" width="130" height="86" alt="A couple embraces at a memorial on Boylston Street to the victims of the Boston Marathon bombings in Boston" align="left" title="A couple embraces at a memorial on Boylston Street to the victims of the Boston Marathon bombings in Boston" border="0" /&gt;&lt;/a&gt;Some lawmakers fault the FBI for not following up on intelligence about one of the alleged bombers.&lt;/p&gt;&lt;br clear="all"/&gt;</media:text><media:credit role="publishing company"></media:credit></item></guid></channel> 
</rss> 

我feedViewController.m如下:

@interface feedViewController() 

    @end 

    @implementation feedViewController 

    @synthesize tableView,showPosts; 
    //@synthesize feed; 

    Feed *xmlParseFeed; 
    //channel *chShow; 

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    { 
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 


    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     //self.feed = [[Feed alloc] init]; 
     self.showPosts = [[NSMutableArray alloc] init]; 

     NSArray *newPosts = [xmlParseFeed newPosts]; 

     if (newPosts) { 
      [self.showPosts addObjectsFromArray:newPosts]; 
      //[newPosts release]; 
     } 

     static NSString *feedURLString = @"http://news.yahoo.com/rss"; 
     NSURL *feedURL = [NSURL URLWithString:feedURLString]; 
     xmlParseFeed = [[Feed alloc] initWithURL:feedURL]; 
     self.showPosts = [[NSMutableArray alloc] init]; 

     [xmlParseFeed refresh]; 
    } 

    - (void)viewDidUnload 
    { 
     [self setTableView:nil]; 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
    } 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 
    - (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section { 

     return [showPosts 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] ; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone;  
     } 

     ItemDataCollection *post = [showPosts objectAtIndex:indexPath.row]; 
     Feed *entry = [showPosts objectAtIndex:indexPath.row]; 
     // Set up the cell... 
      channel *channelRecord = [self.showPosts objectAtIndex:indexPath.row]; 
     cell.textLabel.text = channelRecord.cTitle; 
     cell.textLabel.text = channelRecord.pubDate; 
     NSLog(@"channelRecord.title *****************%@",channelRecord.cTitle);  

     /* cell.textLabel.text = post.title; 
     cell.textLabel.text = post.itemDescription; 
     cell.textLabel.text = post.titleImage; 
     NSLog(@"item title******%@",post.title); */ 




     cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12]; 
     cell.textLabel.numberOfLines = 1; 





     return cell; 
    } 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

Properties in channel class are: cTitle,pubDate and itemCollectionArray. Properties in Post class are: title, itemDescription and titleImage. Feed class is for parsing. the contents of Feed.m is as follows: 

@class ItemDataCollection; 
    @implementation Feed 
    @synthesize feedURL,feedRequest,currentElement,currentElementData; 
    //@synthesize feedChannel; 
    //@synthesize feedPosts; 
    static ItemDataCollection *itemCollection; 
    static NSMutableArray *channelCollection; 
    NSMutableString *mString; 
    NSMutableString *str; 
    channel *Channel ; 
    - (NSArray *)newPosts { 
     NSMutableArray *posts = [[NSMutableArray alloc] init]; 

     for (NSInteger item = 1; item <= 5; item++) { 
      //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
      ItemDataCollection *newPost; 
      newPost = [[ItemDataCollection alloc] init]; 
      // NSString *pubDate = [NSDate dateWithTimeIntervalSinceNow:@"pubDate"]; 
     newPost.title = [NSString stringWithFormat:@"title %@", kTitleElementName]; 
      newPost.itemDescription = [NSString stringWithFormat:@"itemdescription %@", kItemDescription]; 
      newPost.titleImage = [NSString stringWithFormat:@"image %@",kItemImage]; 
     // newPost.itemTitle = [NSString stringWithFormat:@"itemtitle %@", ITEM_TITLE_XML]; 

      [posts addObject:newPost]; 
      //[newPost release]; 

      // [pool drain]; 
     } 
     return posts; 
    } 


    - (Feed *) initXMLParser { 

     //[super init]; 

     appDelegate = (appAppDelegate *)[[UIApplication sharedApplication] delegate]; 

     return self; 
    } 
    -(id)initWithURL:(NSURL *)sourceURL { 
     if (self = [super init]) { 
      self.feedURL = sourceURL; 

      channelCollection=[[NSMutableArray alloc] init]; 
     } 
     return self; 
    } 
    - (void)refresh { 
     self.feedRequest = [ASIHTTPRequest requestWithURL:feedURL]; 

     // [feedPosts removeAllObjects]; 
     [feedRequest setDelegate:self]; 
     [feedRequest startAsynchronous]; 
    } 
    - (void)requestFinished:(ASIHTTPRequest *)request { 
     NSData *responseData = [request responseData]; 
     // NSLog(@"*************response are *********%@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); 

     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:responseData]; 
     [parser setDelegate:self]; 
     if ([parser parse]) { 

      [[NSNotificationCenter defaultCenter] 
      postNotificationName:@"myevent" 
      object:nil]; 
      // [posts ] 

     }  
    } 

    - (void)requestFailed:(ASIHTTPRequest *)request { 
     NSError *error = [request error]; 
     NSLog(@"requestFailed: %@", [error localizedDescription]); 
    } 

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

     if ([elementName isEqualToString:kChannelElementName]) { 
      Channel = [[channel alloc] init]; 
      dict=[[NSMutableDictionary alloc] init]; 
      [Channel setItemCollectionArray:[[NSMutableArray alloc] init]]; 


      return ; 
    } 

     if ([elementName isEqualToString:kItemElementName]) { 
      itemCollection=[[ItemDataCollection alloc] init]; 


      return ; 
     } 
     if ([elementName isEqualToString:kTitleElementName]) { 


      return ; 

     } 
     if([elementName isEqualToString:kItemDescription]){ 
      return ; 
     } 
     if ([elementName isEqualToString:kItemImage]) { 
      NSString *urlString = attributeDict[@"url"]; 
      if(urlString){ 
       [dict setObject:urlString forKey:@"img"]; 
       NSLog(@"%@",urlString); 
       mString = [NSString stringWithFormat:urlString]; 
       str = [NSString stringWithFormat:mString]; 
      } 

      return ; 
     } 

    } 

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

     if (currentElementData == nil) { 
      self.currentElementData = [[NSMutableString alloc] init]; 
     } 

     [currentElementData appendString:string];  
    } 
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
     namespaceURI:(NSString *)namespaceURI 
    qualifiedName:(NSString *)qName { 

     if ([elementName isEqualToString:kChannelElementName]) { 
      [channelCollection addObject:Channel]; 

      NSLog(@"channel are***********%@",channelCollection); 
      for(ItemDataCollection *mydata in Channel.itemCollectionArray){ 
       NSLog(@"___%@ <><><><><> desc \n <><><><><><> img \n  %@",mydata.title,/*mydata.itemDescription*/mydata.titleImage); 

      } 

      Channel =nil; 

     } 

     else if ([elementName isEqualToString:kItemElementName]) { 
      [[Channel itemCollectionArray] addObject:itemCollection]; 
      itemCollection=nil; 

      } 
     else if ([elementName isEqualToString:kTitleElementName]) { 
      if(itemCollection==nil){ 

      Channel.cTitle=currentElementData; 
      } 
      else{ 

       itemCollection.title=currentElementData; 

      } 



     } 
     else if ([elementName isEqualToString:kPubDate]) { 
      Channel.pubDate=currentElementData; 


     } 
     else if ([elementName isEqualToString: kItemDescription]) { 
      if(itemCollection!=nil){ 
      itemCollection.itemDescription = currentElementData; 


     } 
     } 
     else if([currentElementData rangeOfString:@"media:content"].location){ 

      if(itemCollection!=nil){ 

      if([str isEqualToString:mString]){ 
       // [currentElementData appendString:dict]; 
        itemCollection.titleImage = mString; 
      } else{ 
       itemCollection.titleImage = @""; 
      } 
      } 
     } 



     self.currentElementData = nil; 
    } 
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 

     NSString *info = [NSString stringWithFormat: 
          @"Error %i, Description: %@, Line: %i, Column: %i", 
          [parseError code], 
          [[parser parserError] localizedDescription], 
          [parser lineNumber], 
          [parser columnNumber]]; 

     NSLog(@"RSS Feed Parse Error: %@", info); 
    } 

    @end 

飼料是越來越解析,並且可以在控制檯上可以看出。但它在屏幕上不可見。可能是什麼原因?正如我前面所說,cellForRowAtIndexPath()根本沒有被訪問。這是什麼原因?

我已經添加了這種方法:

- (void)feedChanged:(NSNotification *)notification { 
    NSArray *newPosts = [xmlParseFeed newPosts]; 

    NSLog(@"%@",newPosts); 
    if (newPosts) { 

     [self.showPosts addObjectsFromArray:newPosts]; 
     [self.tableView reloadData]; 
     // [self updateViewTitle]; 
     // [newPosts release]; 
    } 
+0

一旦檢查數組newPosts是否有值並將數據源添加到tableview? – Balu 2013-04-22 05:43:34

+0

您是否正確設置了代表@ Crazed'nDazed – krishh 2013-04-22 05:52:04

+0

我在viewDidLoad()中打印了newPosts並顯示爲null ....爲什麼這樣?數據源和委託已經使用故事板設置。 – 2013-04-22 07:13:24

回答

0

我看不出在你的代碼是從UITableView中調用reloadData。您需要在之後調用此方法加載newPosts。

如果不工作,檢查兩件事情,如:

  1. 什麼是的tableView返回值:numberOfRowsInSection :?它是否大於零?如果它爲零,那就是爲什麼tableView:cellForRowAtIndexPath不會被調用。

  2. 如何加載Feed?它在主線程還是不同線程中?您應該將其加載到另一個線程中,以便在加載數據時不會阻止UI,但要確保在數據加載到newPosts後調用reloadData。