2012-12-04 47 views
0

我有許多博客應用程序。最近,其中一些人開始遇到一些問題。我使用ASIHTTP Classes和GDataXML Classes來解析wordpress feed的xml,並將每個item(article)放入一個可變數組中。然後tableview應該將所有的故事加載到每篇文章的單元格中。我遇到的問題是新文章沒有在第一次運行時顯示,用戶需要拉到刷新,然後顯示新文章。我剛剛在應用程序上進行了測試。這篇文章是在幾個小時前發佈的。我運行的應用程序,它不在那裏。它表明,拉動刷新。徹底關閉應用程序,重新啓動它,並且它又一次消失了。這裏是TableView中的實現代碼:TableView未顯示博客的所有行

@implementation RootViewController 


- (void)refresh { 
    self.allEntries = [NSMutableArray array]; 
    self.queue = [[[NSOperationQueue alloc] init] autorelease]; 
    self.feeds = [NSArray arrayWithObjects:@"http://bubblycandacebabbles.wordpress.com/?cat=-2008&feed=rss2", 
        nil]; 


    for (NSString *feed in _feeds) { 
     NSURL *url = [NSURL URLWithString:feed]; 
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
     [request setDelegate:self]; 
     [_queue addOperation:request]; 
    } 

} 

- (void)viewDidLoad { 
    [super viewDidLoad];  
    [activity startAnimating]; 
    //[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbarcopy.png"] forBarMetrics:UIBarMetricsDefault]; 
    self.title = @"Blog"; 
     CGFloat nRed=111.0/255.0; 
    CGFloat nBlue=209/255.0; 
    CGFloat nGreen=229.0/255.0; 
    UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1]; 
    UIBarButtonItem *font = [[UIBarButtonItem alloc] initWithTitle:@"Change Font Size" style:UIBarButtonItemStylePlain target:self action:@selector(fontsizes)]; 
    self.navigationController.navigationItem.rightBarButtonItem = font; 
    self.tableView.backgroundColor = myColor; 

    self.refreshControl = [[UIRefreshControl alloc] init]; 

    [self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged]; 
    [self refresh]; 
} 
-(void) refreshInvoked:(id)sender forState:(UIControlState)state { 
    // Refresh table here... 
    [_allEntries removeAllObjects]; 
    [self.tableView reloadData]; 
    [self refresh]; 
} 

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries { 

    NSArray *channels = [rootElement elementsForName:@"channel"]; 
    for (GDataXMLElement *channel in channels) {    

     NSString *blogTitle = [channel valueForChild:@"title"];      

     NSArray *items = [channel elementsForName:@"item"]; 
     for (GDataXMLElement *item in items) { 

      NSString *articleTitle = [item valueForChild:@"title"]; 
      NSString *articleUrl = [item valueForChild:@"link"];    
      NSString *articleDateString = [item valueForChild:@"pubDate"];   
      NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822]; 
      NSString *articleImage = [item valueForChild:@"content:encoded"]; 
      NSScanner *theScanner; 
      NSString *gt =nil; 
      theScanner = [NSScanner scannerWithString:articleImage]; 


      NSString *comments = [articleUrl stringByAppendingString:@"#respond"]; 
      NSString *commentslink = [NSString stringWithFormat: @"<a href=\"%@\">Leave A Comment</a>",comments]; 
      // find start of tag 
      [theScanner scanUpToString:@"alt=\"\" width=" intoString:NULL] ; 

      // find end of tag 
      [theScanner scanUpToString:@"/>" intoString:&gt] ; 
      // replace the found tag with a space 
      //(you can filter multi-spaces out later if you wish) 
      NSString *test = [articleImage stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@", gt] withString:@"alt=\"\" width=\"150\" height=\"150\""]; 
      NSString *final = [test stringByReplacingOccurrencesOfString:@"float:none;height:30px" withString:@"float:none;height:1px"]; 
      NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
      [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
      [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
      NSString *dateofarticle = [dateFormatter stringFromDate:articleDate]; 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
      NSString *smalltitle = [defaults objectForKey:@"Title"]; 
      NSString *smallbody = [defaults objectForKey:@"Article"]; 
      NSString *bodyoftext = [[[[[[[[[[[@"<head><body bgcolor=\"#6fd1e5\" text=\"#CC0099\"><style type='text/css'>a > img {pointer-events: none;cursor: default;max-width: 310;}</style></head><b><font size=" stringByAppendingString: smalltitle ] stringByAppendingString:@"><div align=\"left\"><FONT FACE=\"noteworthy\">" ]stringByAppendingString:articleTitle] stringByAppendingString:@"</font></b><font size=" ] stringByAppendingString:smallbody ] stringByAppendingString:@"><div align=\"left\"><FONT FACE=\"noteworthy\">"] stringByAppendingString:dateofarticle] stringByAppendingString:@"</div></p><FONT FACE=\"noteworthy\">"] stringByAppendingString:final] stringByAppendingString:commentslink]stringByAppendingString:@"</FONT>"]; 








      RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                 articleTitle:articleTitle 
                 articleUrl:articleUrl 
                 articleDate:articleDate 
                 articleImage:bodyoftext] autorelease]; 
      [entries addObject:entry]; 
     }  
    } 

} 


- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries { 

    if ([rootElement.name compare:@"rss"] == NSOrderedSame) { 
     [self parseRss:rootElement entries:entries]; 
    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {      
     [self parseAtom:rootElement entries:entries]; 
    } else { 
     NSLog(@"Unsupported root element: %@", rootElement.name); 
    }  
} 


// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [_allEntries count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 



    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 
    NSString *substring = @"http://bubblycandacebabbles.files.wordpress.com"; 
    NSRange textRange = [entry.articleImage rangeOfString:substring]; 

    if(textRange.location != NSNotFound){ 
     NSString *thearticleImage = entry.articleImage; 
     NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\"" options:NSRegularExpressionCaseInsensitive error:NULL]; 
     NSString *someString = thearticleImage; 
     NSString *oneurl = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]]; 
     NSString *finalstring = [oneurl stringByReplacingOccurrencesOfString:@"src=\"" withString:@""]; 
     NSString *thefinalstring = [finalstring stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
     [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
     [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
     CGFloat nRed=204.0/255.0; 
     CGFloat nBlue=0/255.0; 
     CGFloat nGreen=153.0/255.0; 
     UIColor *myColortext=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1]; 
     NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate]; 
     UIFont *cellFont = [UIFont fontWithName:@"noteworthy" size:16]; 
     UIFont *cellFont2 = [UIFont fontWithName:@"noteworthy" size:12];  
     CALayer * l = [cell.imageView layer]; 
     [l setMasksToBounds:YES]; 
     [l setCornerRadius:11]; 
     [l setBorderWidth:2.0]; 
     [l setBorderColor:[[UIColor blackColor] CGColor]]; 
     cell.textLabel.text = entry.articleTitle; 
     cell.textLabel.numberOfLines = 2; 
     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; 

     cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - Mother May I Blog", articleDateString]; 

     cell.textLabel.font = cellFont; 
     cell.detailTextLabel.font = cellFont2; 
     cell.textLabel.textColor = myColortext; 
     cell.detailTextLabel.textColor = myColortext; 
     [cell.imageView setImageWithURL:[NSURL URLWithString:thefinalstring] placeholderImage:[UIImage imageNamed:@"[email protected]"]]; 


    } 
    else { 
     CALayer * l = [cell.imageView layer]; 
     [l setMasksToBounds:YES]; 
     [l setCornerRadius:11]; 
     [l setBorderWidth:2.0]; 
     [l setBorderColor:[[UIColor blackColor] CGColor]]; 
     NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
     [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
     [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
     NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate]; 
     UIFont *cellFont = [UIFont fontWithName:@"noteworthy" size:16];  
     UIFont *cellFont2 = [UIFont fontWithName:@"noteworthy" size:12];  
     cell.imageView.image = [UIImage imageNamed:@"[email protected]"]; 
     cell.textLabel.text = entry.articleTitle; 
     cell.textLabel.numberOfLines = 2; 
     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; 

     cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - Mother May I Blog", articleDateString]; 
     CGFloat nRed=204.0/255.0; 
     CGFloat nBlue=0/255.0; 
     CGFloat nGreen=153.0/255.0; 
     UIColor *myColortext=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1]; 
     cell.textLabel.font = cellFont; 
     cell.detailTextLabel.font = cellFont2; 
     cell.textLabel.textColor = myColortext; 
     cell.detailTextLabel.textColor = myColortext; 


    } 


    return cell; 
} 
+0

你真的需要修剪積極如果你希望在這個論壇上得到幫助,請下載代碼 –

+0

@DanF好了,我將其修剪到我的問題的要領 – user717452

回答

0

您需要在某一時刻調用

[self.tableView reloadData]; 

後您的數據加載完畢。你如何做到這一點有點棘手,你需要一些方法來告訴你的操作隊列是否爲空。或者,您可以在理論上在隊列中的每個操作完成之後調用它,這樣表一次將填充一個表。這可能會導致問題,如果用戶連接速度慢,因爲重裝表可能會導致用戶體驗的跳躍,我不是從不同的線程調用reloadData的線程安全

+0

即使第一次啓動並且tableview爲空,我也必須調用reloadData。 – user717452

+0

您的數據加載完成後調用它,這將強制更新單元格。它在表出現時會自動調用一次,除非手動調用它,否則不會再次調用它。 –

+0

那麼,那麼對用戶來說,它不會加載一切,然後再做一次? – user717452