在tableview显示之前添加加载视图控制器

时间:2013-10-31 12:08:38

标签: ios objective-c uitableview uistoryboard uiactivityindicatorview

我有一个tableView控制器,可以加载我解析的XML数据。这需要一些时间。

- (void)viewDidLoad
{
[super viewDidLoad];

CustomStringParser *customStringParser = [[CustomStringParser alloc] init];

// Set MORE logo in navigation bar
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Navigation"]];

// Download and parse XML data
RXMLElement *rxml = [RXMLElement elementFromXMLData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.morecobalt.co.uk/rss/?t=events"]]];

// Create an array to store each feed
eventsFeeds = [[NSMutableArray alloc] init];

// Loop through XML data
[rxml iterate:@"channel" usingBlock:^(RXMLElement *supportElement) {
    [supportElement iterate:@"item" usingBlock:^(RXMLElement *repElement) {

        // Assign element to string
        NSString *title = [repElement child:@"title"].text;
        NSString *subtitle = [repElement child:@"tagline"].text;
        NSString *description = [repElement child:@"description"].text;
        NSString *imageurl = [repElement child:@"image"].text;
        NSString *startDate = [repElement child:@"start_date"].text;
        NSString *endDate = [repElement child:@"end_date"].text;

        // Assign element value to MoreCobalt.h propertys
        currentFeed = [MoreCobaltEvents alloc];
        currentFeed.title = title;
        currentFeed.subtitle = subtitle;
        currentFeed.imageurl = imageurl;

        // DESCRIPTION FORMATTING
        description = [customStringParser parseHTML:description];
        description = [customStringParser parseLinesMultiple:description];
        description = [customStringParser removeSocialSignifiers:description];
        currentFeed.description = description;

        // DATE FORMATTING
        currentFeed.startDate = [customStringParser formatDate:startDate];
        currentFeed.endDate = [customStringParser formatDate:endDate];

        // Add a new object to the feeds array
        [eventsFeeds addObject:currentFeed];
    }];
}];
}

在解析数据时,我想添加一个带有活动指示器的加载屏幕。我猜这将是一个子视图。我怎么能这样做?

注意:我正在使用故事板。图片

enter image description here

2 个答案:

答案 0 :(得分:2)

只需拖动xib上的活动指示器组件即可。 在.h文件中声明属性。把它连起来。

在viewDidLoad调用的.m文件中。

[self.activityIndicator starAnimating];
[self.activityIndicator.hidesWhenStopped = YES;

然后在你的表加载后调用:

[self.activityIndicator stopAnimating];

无需子视图。

答案 1 :(得分:-1)

Write below code in **AppDelegate.h** 
 UIActivityIndicatorView *progressIndicator;
    UIView *progressIndicatorView;


Write below code in **AppDelegate.m** 

-(void) addProgressIndicator:(UIView *)parentView
{
    [self.progressIndicatorView removeFromSuperview];

        self.progressIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(0,0, parentView.frame.size.width,parentView.frame.size.height)];

        self.progressIndicatorView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((parentView.frame.size.width - 70) / 2,(parentView.frame.size.height - 70) / 2,70,70)];

    [self.progressIndicatorView addSubview:self.progressIndicator];
    [parentView addSubview:self.progressIndicatorView];
    [parentView bringSubviewToFront:self.progressIndicatorView];
    [self.progressIndicatorView bringSubviewToFront:self.progressIndicator];
    [self.progressIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.progressIndicator setHidesWhenStopped:YES];
    [self.progressIndicator stopAnimating];
    [self.progressIndicatorView setHidden:YES];
}

-(void) startProgressIndicator
{
    [NSThread detachNewThreadSelector:@selector(showProgressIndicator) toTarget:self withObject:nil];
}

-(void) showProgressIndicator
{
    [self.progressIndicatorView setHidden:NO];
    [self.progressIndicator startAnimating];
}

-(void) stopProgressIndicator
{
    [self.progressIndicator stopAnimating];
    [self.progressIndicatorView setHidden:YES];
}

在viewDidLoad / ViewWillAppear中调用此方法以显示进度指示器。 祝好运 !!

相关问题