2011-07-26 35 views
7

我目前加載數據從JSON服務未來在viewDidLoad方法UITableViewController。問題是數據需要時間來檢索和解析,視圖需要時間才能創建。 加載此數據的最佳位置在哪裏?我認爲在創建視圖之後有一個用於加載數據的鉤子。通過這樣做,我將能夠在最終視圖中使用一些UIActivityIndicatorView。 感謝何時在UITableViewController中加載數據?

回答

1

我想你要問什麼是在一個UITableView從Web服務顯示數據的工作流程。

下面是我建議:

  • viewDidLoad使得NSURLRequest您的JSON文件。還 增加了一個加載視圖到當前視圖(I使用與 黑BG(0.5阿爾法一個UIView),加標籤和UIActivityIndi​​cator)。在這種 方法還設置一個BOOL伊娃(你需要在你的 頭添加)稱爲loaded爲NO。

  • 您可以將您的NSURLRequest數據連接成可變數據對象 。

  • 當您的NSURLRequest完成時,將其數據轉換爲字符串 ,並將JSON解析爲某種排列的數組(或字典,如果您想要的話)。用同樣的方法刪除加載視圖,並將 布爾值loaded更改爲YES。然後你告訴的tableView重新加載 它的數據:[self.tableView reloadData];

這裏的地方,奇蹟發生了......在你的表視圖方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (loaded) return [myArrayOfJSONObjects count]; 
    return 0; // Will only return 0 if the data is not downloaded 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    if (loaded) { 
     cell.textLabel.text = [myArrayOfParsedJSONObjects objectAtIndex:row]; 
     //Anything else you want to set 
    } 
    else { 
     //Do nothing :) - you shouldn't reach this else anyway because your numberOfRows method should stop it 
    } 
} 
+0

不完全是。這是我已經做的。我在viewDidLoad上加載數據並在cellForRowAtIndexPath中填充表。它的工作原理,但我所說的是,在這種情況下,如果JSON服務需要10秒鐘才能回覆,則視圖會在用戶選擇10秒後出現。在我的情況下,當用戶點擊一行時,視圖從另一個表視圖加載。 –

+0

那麼你問有沒有可以加載它的地方? –

+0

我可以在任何地方加載它,但我想要做的是:1 /顯示視圖(可能爲空表)2 /自動加載數據從遠程服務2'/加載時顯示一些等待消息 –

0

您可以打開新的視圖,並顯示與消息UIActivityIndi​​cator用戶,你爲他加載新的數據。

對我來說,這是最好的選擇,因爲界面保持負責和用戶看到你實際做的東西,應用掛不住。

+0

我打開一個新的查看與消息,在這種情況下我停止這個加載器[tableview reloadData] –

5

最後在這裏是一種基於註釋的解決方案:啓動一個線程在viewDidLoad中沒有阻止所有得到的數據:

- (void) viewDidLoad 
{ 
    dataLoaded = NO; 

    [self initSpinner]; 
    [self launchLoadData]; 
... 
} 

-(void)launchLoadData { 
    NSLog(@"Launching thread"); 
    [NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil]; 
} 

- (void) loadData { 
    dataLoaded = NO; 
    NSLog(@" thread launched"); 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    [self loadDataFromURL:nil]; 
    dataLoaded = YES; 
    [self.tableView reloadData]; 
    [pool release]; 
} 

- (void)loadDataFromURL:(NSString*)url { 
    // start the spinner to show that loading may be time consuming... 
    [NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil]; 
    JSONLoader *loader = [[JSONLoader alloc] init]; 
    self.accounts = [loader getAccountsFromURL:@"http://foo/bar/repository.json"]; 
    [loader release]; 
    //[NSThread sleepForTimeInterval:3]; 
    [NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil]; 
} 

和使用標誌的表是否顯示數據。當從線程調用時,tableView reloadData將完成其餘的工作。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (dataLoaded) return [self.accounts count]; 
    return 0; 
} 
相關問題