2013-11-01 21 views
0

我已經構建了一個使用故事板的iOS 7應用程序。在我的offersViewController內,我有一個UIView和一個UITableView。 UIView作爲一個子視圖,在我的feed被解析的時候顯示一個加載消息。一旦完成子視圖被刪除,我的分析數據呈現在我的UITableView。TableView重新加載,當你離開然後返回

@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];。我需要這個或者表格變空。我究竟做錯了什麼?

+0

什麼都沒有。但你不一定需要'viewDidAppear:'中的代碼...... – Wain

+0

當然,它應該在-viewDidLoad中完成嗎?每當視圖出現時,tableView就會被重新加載,而不是一次,即在viewDidLoad中。 –

+0

您正在重載tableView in viewDidAppear ...並且它意味着它會在每次出現視圖時重新載入數據...現在,如果您需要每次在視圖出現時重新載入tableView,請在ViewWillAppear中使用此代碼。如果你只想加載一次數據。拿這個代碼來viewDidLoad ... –

回答

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]; 
} 
+0

謝謝。我也嘗試過這種方式。 「loadin」g UIView不再出現。 – user2920762

+0

它不會顯示,因爲數據已經加載。只有第一次加載視圖時,ViewDidLoad中的代碼纔會運行。所有其他時間(導航回該視圖等)viewDidLoad不會被調用。 –

+0

那麼這裏的修正是什麼? – user2920762

0

很少沒有價值的東西:

  1. 你應該提取所有的代碼解析你的XML到另一個班級。您不應該在視圖控制器內部解析來自XML的數據。

  2. 現在您正在運行網絡調用並在每次出現視圖時解析XML數據。你可能會認爲只有做它時,認爲負載,從而運行你的代碼viewDidLoad

  3. 當你最終感動你可以考慮有一個臨時的本地陣列和填充,隨着XML返回值的代碼,然後使用isEqualToArray將其與您的值的屬性進行比較,看看它的不同。如果是,重新加載表並重新設置屬性,如果沒有,則不要。

+0

感謝您的建議,但這並沒有真正回答我的問題。當我弄明白這一點時,我會將XML解析移到一個單獨的類中。 – user2920762

+0

請重新閱讀編號2.您正在運行您每次出現視圖時發佈的內容。當您從視圖控制器離開時返回,視圖出現,從而運行網絡調用和表重新加載。 –

+0

謝謝彼得,我試着把代碼放在viewDidLoad中,現在加載屏幕不顯示 – user2920762

相關問題