0

我有许多博客应用程序。最近,他们中的一些人开始遇到一些问题。我使用 ASIHTTP 类和 GDataXML 类来解析 wordpress 提要的 xml,并将每个项目(文章)放入一个可变数组中。然后,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:@"iphoneicon@2x.png"]];


    }
   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:@"iphoneicon@2x.png"];
        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;
}
4

1 回答 1

0

你需要调用

[self.tableView reloadData];

在您的数据完成加载后的某个时间点。你如何做到这一点有点棘手,你需要某种方式来判断你的操作队列何时为空。或者,理论上您可以在队列中的每个操作完成后调用它,这样表将一次填充一个。如果用户连接速度较慢,这可能会导致问题,因为重新加载表可能会导致用户体验跳跃,而且我对reloadData从不同线程调用的线程安全性并不乐观

于 2012-12-05T14:15:49.793 回答