活动指标未显示

时间:2013-08-06 13:58:11

标签: ios uiactivityindicatorview

我有一个表格视图,可以从RSS源获取数据。我正在尝试在我的iPad应用中加载表格视图时显示我的活动指示器。它在iPhone上运行良好,但指示灯永远不会出现在iPad上。有什么建议吗?

@implementation ActionAlertsIpad
{
    UIActivityIndicatorView *loadingIndicator;
}

在viewDidLoad中:

CGFloat width = CGRectGetWidth(self.view.bounds);
    CGFloat height = CGRectGetHeight(self.view.bounds);

loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loadingIndicator.center = CGPointMake(width/2, height/2);
    loadingIndicator.hidesWhenStopped = YES;
    [self.view addSubview:loadingIndicator];
    [loadingIndicator startAnimating];

在connectionDidFinishLoading中:

[loadingIndicator stopAnimating];

编辑:我被要求提供更多代码,因此以下是与此相关的所有代码。

#import "ActionAlertsIpad.h"
#import "RSSChannel.h"
#import "RSSItem.h"
#import "WebViewControllerIpad.h"
// #import "CustomCellBackground.h"
#import "DTCustomColoredAccessory.h"
#import "KFBAppDelegate.h"

@implementation ActionAlertsIpad
{
    UIActivityIndicatorView *loadingIndicator;
}
@synthesize webViewController;

- (void)viewDidLoad
{
    UIImageView *background = [[UIImageView alloc]init];
    background.image = [UIImage imageNamed:@"plain_app-background.png"];

    CGFloat width = CGRectGetWidth(self.view.bounds);
    CGFloat height = CGRectGetHeight(self.view.bounds);

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.tableView setBackgroundView:background];
    self.title = @"Action Alerts";
    loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loadingIndicator.center = CGPointMake(width/2, height/2);
    loadingIndicator.hidesWhenStopped = YES;
    [self.view addSubview:loadingIndicator];
    [loadingIndicator startAnimating];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"%@ found a %@ element", self, elementName);
    if ([elementName isEqual:@"channel"])
    {
        // If the parser saw a channel, create new instance, store in our ivar
        channel = [[RSSChannel alloc]init];

        // Give the channel object a pointer back to ourselves for later
        [channel setParentParserDelegate:self];

        // Set the parser's delegate to the channel object
        [parser setDelegate:channel];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return 0;
    NSLog(@"channel items %d", [[channel items]count]);
    return [[channel items]count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // return nil;

    UIImageView *image = [[UIImageView alloc]init];
    image.image = [UIImage imageNamed:@"CellImage.png"];

    UIImageView *background = [[UIImageView alloc]init];
    background.image = [UIImage imageNamed:@"plain_app-background.png"];

    UIImageView *highlightedCellImage = [[UIImageView alloc]init];
    highlightedCellImage.image = [UIImage imageNamed:@"HighlightedCellImage"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
        cell.textLabel.font=[UIFont systemFontOfSize:16.0];
    }
    RSSItem *item = [[channel items]objectAtIndex:[indexPath row]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ssZ"];
    NSDate *pubDate = [formatter dateFromString:[item date]];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *dateString = [formatter stringFromDate:pubDate];
    NSLog(@"Date String: %@", dateString);

    [[cell textLabel]setText:[item title]];
    [[cell detailTextLabel]setText:dateString];

    NSLog(@"Date: %@", [item date]);

    tableView.backgroundColor = [UIColor clearColor];
    // cell.backgroundView = [[CustomCellBackground alloc] init];
    // cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.highlightedTextColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:24.0];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.highlightedTextColor = [UIColor blueColor];
    cell.detailTextLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:18.0];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.backgroundView = image;
    cell.selectedBackgroundView = highlightedCellImage;
    tableView.backgroundView = background;

    DTCustomColoredAccessory *accessory = [DTCustomColoredAccessory accessoryWithColor:cell.textLabel.textColor];
    accessory.highlightedColor = [UIColor blueColor];
    cell.accessoryView =accessory;

    return cell;
}

- (void)fetchEntries
{
    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc]init];

    // Construct a URL that will ask the service for what you want -
    // note we can concatenate literal strings together on multiple lines in this way - this results in a single NSString instance
    NSURL *url = [NSURL URLWithString:@"http://kyfbnewsroom.com/category/public-affairs/notifications/feed/"];

    // Put that URL into an NSURLRequest
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Create a connection that will exchange this request for data from the URL
    connection = [[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:YES];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];

    if (self)
    {
        [self fetchEntries];
    }

    return self;
}

// This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
    // Add the incoming chunk of data to the container we are keeping
    // The data always comes in the correct order
    [xmlData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
    /* We are just checking to make sure we are getting the XML
     NSString *xmlCheck = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding];
     NSLog(@"xmlCheck = %@", xmlCheck);*/

    [loadingIndicator stopAnimating];

    // Create the parser object with the data received from the web service
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];

    // Give it a delegate - ignore the warning here for now
    [parser setDelegate:self];

    //Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking
    [parser parse];

    // Get rid of the XML data as we no longer need it
    xmlData = nil;

    // Reload the table.. for now, the table will be empty
    NSMutableArray *notActionAlerts = [NSMutableArray array];
    for (RSSItem *object in channel.items) {
        if (!object.isActionAlert) {
            [notActionAlerts addObject:object];
        }
    }

    for (RSSItem *object in notActionAlerts) {
        [channel.items removeObject:object];
    }

    [[self tableView]reloadData];

    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);
}

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{
    // Release the connection object, we're done with it
    connection = nil;

    // Release the xmlData object, we're done with it
    xmlData = nil;

    // Grab the description of the error object passed to us
    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]];

    // Create and show an alert view with this error displayed
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [av show];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy];

    UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:webViewController];

    [details replaceObjectAtIndex:1 withObject:detailNav];

    KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
    appDelegate.splitViewController.viewControllers = details;
    appDelegate.window.rootViewController = self.splitViewController;
    appDelegate.splitViewController.delegate = webViewController;
    [appDelegate.splitViewController viewWillAppear:YES];

    // Grab the selected item
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];

    // Construct a URL with the link string of the item
    NSURL *url = [NSURL URLWithString:[entry link]];

    // Construct a request object with that URL
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Load the request into the web view
    [[webViewController webView]loadRequest:req];
    webViewController.hackyURL = url;

    // Set the title of the web view controller's navigation item
    [[webViewController navigationItem]setTitle:[entry title]];
}

@end

1 个答案:

答案 0 :(得分:0)

同样的代码在Ipad中也适合我。

可能您已在视图中添加了一些子视图,例如i-Pad的活动指示器。

如果可能的话,你可以再分享一些代码。

检查你使用的iphone和ipad的if - else条件