Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当您离开然后返回时,会重新加载TableView_Ios_Objective C_Uiviewcontroller_Uitableview - Fatal编程技术网

Ios 当您离开然后返回时,会重新加载TableView

Ios 当您离开然后返回时,会重新加载TableView,ios,objective-c,uiviewcontroller,uitableview,Ios,Objective C,Uiviewcontroller,Uitableview,我用故事板构建了一个iOS 7应用程序。在我的OfferView控制器中,我有一个UIView和一个UITableView。UIView充当一个子视图,在分析我的提要时显示加载消息。完成后,将删除子视图,并在UITableView中显示已解析的数据 @interface OffersViewController () @end @implementation OffersViewController @synthesize loadingView; MoreCobaltOffers *cu

我用故事板构建了一个iOS 7应用程序。在我的OfferView控制器中,我有一个UIView和一个UITableView。UIView充当一个子视图,在分析我的提要时显示加载消息。完成后,将删除子视图,并在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]。我需要这个,否则桌子就空了。我做错了什么?

视图将在视图出现时调用。为了避免每次重新加载表格,请将代码移到ViewDidDisplay中

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

- (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];
}

没有什么东西一文不值:

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

  • 现在,您正在运行网络调用,并在每次视图出现时解析XML数据。您可以考虑的只是在视图加载时执行它,从而在<代码>视图Dead Load

  • 中运行代码。 >p>每当您移动代码时,您可能会考虑使用一个临时的本地数组并用XML返回值填充它,然后使用<代码> ISQualtoToSux/Cuff>将其与值的属性进行比较,以查看其是否不同。如果是,请重新加载表并重新设置属性,如果不是,请不要


    没有什么。但是您不一定需要在
    viewdide:
    中显示该代码……当然应该在-viewdideload中执行吗?每当视图出现时都会重新加载tableView,而不是仅在viewDidLoad中加载一次。您正在ViewDidDisplay中重新加载tableView。。。这意味着每次视图出现时它都会重新加载数据。。。现在,如果您需要在每次视图出现时重新加载tableView,请在ViewWillDisplay中使用此代码。如果只想加载一次数据。将此代码带到viewDidLoad…如果我将代码放在viewDidLoad中,“加载”UIView将不再显示谢谢。我也试过这种方法。“loadin”UIG视图不再显示。它不会显示,因为数据已加载。只有第一次加载视图时,ViewDidLoad中的代码才会运行。所有其他时间(导航回该视图等)都不会调用viewDidLoad。那么这里的修复方法是什么?为什么每次进入视图时,即使数据已经加载,也希望加载程序显示应用程序?您的权利行为不正确。它应该在viewDidLoad的内部。。。但是,如果我把它放在那里,我就不会再看到加载屏幕了。谢谢你的建议,但这并不能真正回答我的问题。当我弄明白这一点时,我将把xml解析移到一个单独的类中。请重新阅读第2条。您正在运行每次视图出现时发布的内容。当您离开视图控制器并返回时,视图会出现,因此会运行网络调用和表重新加载。谢谢Peter,我尝试将代码放入viewDidLoad,但现在加载屏幕不会出现