重新加载时UITableView会闪烁

时间:2013-10-31 18:17:28

标签: ios objective-c uitableview uiview uiviewcontroller

在我的UIViewController上,我有一个UIView和一个UITableView。 UIView充当加载屏幕,而我的XML Parser(RaptureXML)正在解析表数据。完成后,它会在UITableView中显示数据。

然而,当我运行应用程序并导航到另一个视图控制器然后返回时,由于[self.tableView reloadData];,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.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.tableView reloadData];
}];
}

2 个答案:

答案 0 :(得分:2)

您正在TableView之前添加视图。只需更改界面构建器中的顺序即可。

您在这里查看层次结构:

View
  View
     GrayActivity Indicator
     Label  - Loading
  Table View
    TableViewCell

以下是假设:

View
  Table View
    TableViewCell
  View
     GrayActivity Indicator
     Label  - Loading

修改

请删除此行`[loadingView setHidden:YES];将您的代码更新为以下内容:

[rxml iterate:@"channel" usingBlock:^(RXMLElement *supportElement) {

      [supportElement iterate:@"item" usingBlock:^(RXMLElement *repElement) {

         .
         .
         .

      [[appDelegate offersFeeds] addObject:currentFeed];
   }];
   dispatch_sync(dispatch_get_main_queue(), ^{
      [loadingView setHidden:YES];
      [self.table reloadData];
});

}];

答案 1 :(得分:0)

像这样重构您的代码,我认为它应该可行

// Loop Through XML Data
[rxml iterate:@"channel" usingBlock:^(RXMLElement *supportElement) {
[supportElement iterate:@"item" usingBlock:^(RXMLElement *repElement) {
[loadingView setHidden:No];
...
...
...
...
...
...
[loadingView setHidden:YES];

}];
}];