导航然后返回时重新加载TableView

时间:2013-11-01 11:35:06

标签: ios objective-c uiviewcontroller uitableview

我使用故事板构建了一个iOS 7应用程序。在我的offersViewController中,我有一个UIView和一个UITableView。 UIView充当子视图,在解析我的feed时显示加载消息。完成后,子视图将被删除,我的解析数据将显示在我的UITableView中。

@interface OffersViewController ()

@end

@implementation OffersViewController
@synthesize loadingView;

MoreCobaltOffers *currentFeed;
AppDelegate *appDelegate;

- (void)viewDidAppear:(BOOL)animated
{
    [self.tableView addSubview:loadingView];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Navigation"]];

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

    // Download and parse XML data
    RXMLElement *rxml = [RXMLElement elementFromXMLData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myrssfeed.com"]]];

    // Create an reference to AppDelegate
    appDelegate = [[UIApplication sharedApplication] delegate];

    // Create an array to store each feed
    appDelegate.offersFeeds = [[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 *address = [repElement child:@"address"].text;

            // Assign element value to MoreCobalt.h propertys
            currentFeed = [MoreCobaltOffers alloc];
            currentFeed.title = title;
            currentFeed.imageurl = imageurl;
            currentFeed.addressline = address;

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

            // SUBTITLE FORMATTING
            subtitle = [customStringParser parseHTML:subtitle];
            subtitle = [customStringParser parseLinesSingle:subtitle];
            subtitle = [customStringParser removeSocialSignifiers:subtitle];
            currentFeed.subtitle = subtitle;

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

        //Remove the loading screen
        [loadingView removeFromSuperview];

        //Show table data, if this is not here the table is empty.
        [self.tableView reloadData];
    }];
}

当我运行应用程序时,会出现加载屏幕,然后显示包含数据的表格。如果我从这个视图控制器导航到另一个选项卡,然后导航回来,表格将闪烁。用户体验不是很好。

负责的代码行是[self.tableView reloadData];。我需要这个或表变空。我做错了什么?

2 个答案:

答案 0 :(得分:1)

只要视图出现,就会调用ViewWillAppear。为了不每次重新加载表,请在viewDidAppear中移动代码。

为了显示加载视图,只有一次将解析移动到另一个方法,如:

- (void)parseFeed {
    [self.loadingIndicator startAnimating];
    self.loadingIndicator.hidesWhenStopped = YES;
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Navigation"]];

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

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

    // Create an reference to AppDelegate
    appDelegate = [[UIApplication sharedApplication] delegate];

    // Create an array to store each feed
    appDelegate.offersFeeds = [[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 *address = [repElement child:@"address"].text;

            // Assign element value to MoreCobalt.h propertys
            currentFeed = [MoreCobaltOffers alloc];
            currentFeed.title = title;
            currentFeed.imageurl = imageurl;
            currentFeed.addressline = address;

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

            // SUBTITLE FORMATTING
            subtitle = [customStringParser parseHTML:subtitle];
            subtitle = [customStringParser parseLinesSingle:subtitle];
            subtitle = [customStringParser removeSocialSignifiers:subtitle];
            currentFeed.subtitle = subtitle;

            // Add a new object to the feeds array
            [[appDelegate offersFeeds] addObject:currentFeed];
        }];
        [loadingView removeFromSuperview];
        [self.loadingIndicator stopAnimating];
        [self.tableView reloadData];
    }];
    [loadingView removeFromSuperview];
    [self.loadingIndicator stopAnimating];
    isFirstLoad = NO;
}

声明BOOL以检查它是否是第一次加载并执行此检查:

-(void)viewDidAppear:(BOOL)animated {
    if (isFirstLoad){
    [self parseFeed];
    }
    [super viewDidAppear:animated];
}

答案 1 :(得分:0)

很少有东西值得一试:

  1. 您应该将用于解析XML的所有代码提取到另一个类中。您不应该在视图控制器中解析XML中的数据。

  2. 现在,每次出现视图时,您都在运行网络调用并解析XML数据。您可能会考虑的是仅在加载视图时执行此操作,从而在viewDidLoad

  3. 中运行代码
  4. 每当您最终移动代码时,您可能会考虑使用临时本地数组并使用XML返回值填充它,然后使用isEqualToArray将其与值的属性进行比较,以查看它是否与。如果是,重新加载表并重新设置属性,如果不是,则不要。