简体   繁体   English

TableView不显示博客的所有行

[英]TableView Not Showing All Rows For Blog

I have a number of blog apps. 我有许多博客应用程序。 Recently, some of them started experiencing some issues. 最近,其中一些人开始遇到一些问题。 I use ASIHTTP Classes and GDataXML Classes to parse the xml of a wordpress feed, and put each item (article) into a mutable array. 我使用ASIHTTP类和GDataXML类来解析wordpress feed的xml,并将每个项目(文章)放入一个可变数组中。 The tableview is then supposed to load all of the stories into a cell for each article. 然后,应该使用tableview将所有故事加载到每个文章的单元格中。 The issue I am having is that new articles are not being displayed on first run, the user is having to Pull to Refresh, and then the new article displays. 我遇到的问题是,新的物品没有被显示在第一次运行时,用户不得不拉来刷新,然后将新的文章显示。 I ran a test just now on an app. 我刚刚在一个应用上进行了测试。 The article was posted a few hours ago. 这篇文章是几个小时前发布的。 I ran the app, it wasn't there. 我运行了该应用程序,但它不存在。 Pulled to refresh, it showed. 拉刷新,它显示。 Closed down the app completely, restarted it, and it was gone again. 完全关闭应用程序,然后重新启动,然后它又消失了。 Here is the code in the TableView's implementation: 这是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;
}

You need to invoke 您需要调用

[self.tableView reloadData];

at some point after your data has finished loading. 在数据加载完成后的某个时候。 How exactly you do this is a bit tricky, you need some way of telling when your operation queue is empty. 如何精确地执行此操作有些棘手,您需要某种方法来告知您的操作队列何时为空。 Alternatively, you could theoretically call it after each operation in the queue is complete, that way the table will populate one at a time. 或者,您可以在理论上在队列中的每个操作完成后调用它,这样表将一次填充一个。 This could cause problems if the user is on a slow connection, as reloading the table can cause jumps in the user experience, and I'm not positive on the thread safety of calling reloadData from a different thread 如果用户连接速度慢,这可能会导致问题,因为重新加载表可能会导致用户体验的跳跃,而且我对从其他线程调用reloadData的线程安全性也reloadData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM