0

我已经使用故事板构建了一个 iOS 7 应用程序。在我的 offerViewController 中,我有一个 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];. 我需要这个,否则表会变空。我究竟做错了什么?

4

2 回答 2

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];
}
于 2013-11-01T11:44:21.903 回答
0

几件不值钱的事:

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

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

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

于 2013-11-01T11:48:15.247 回答