2015-02-24 95 views
0

我在2個視圖控制器中有2個表視圖。當我從第一個屏幕選擇一個單元格時,我想加載第二個表格,但是我的問題是:表格是空的,並且在我滾動後填充。UITableView僅在滾動後填充

這是我如何填充我的表:

當選定單元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSURL *url = [NSURL URLWithString:@"http://privatereisen.com/"]; 

    ProgViewController *cd = [self.storyboard instantiateViewControllerWithIdentifier:@"progidentifview"]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

    NSString *path=[NSString stringWithFormat :@"dok/TV/pays/italie/json/chaine%d.json",indexPath.row]; 
    [httpClient postPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 

    {resultofData = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 


     NSString *valuKey=[NSString stringWithFormat :@"chaine%d",indexPath.row]; 
     NSArray *testArray =[resultofData valueForKey:valuKey]; 


     cd.categ=[[NSArray alloc]initWithArray:testArray]; 
     cd.categArray=[[NSArray alloc]initWithArray:self.categoryArray]; 






    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     [[[UIAlertView alloc]initWithTitle:@":(" message:@"Pas d'internet !" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]show]; 
     // NSLog(@"[HTTPClient Error]: %@", error.localizedDescription); 

    }]; 
    [self.navigationController pushViewController:cd animated:YES]; 


    [tableView cellForRowAtIndexPath:indexPath].selected = NO; 


} 

和ProgViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    cprogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifproecell"]; 

    cell.heure.text = [[_categ objectAtIndex:indexPath.row]objectForKey:@"horaire_programme"]; 
    cell.type.text = @""; 
    cell.desc.text = [[_categ objectAtIndex:indexPath.row]objectForKey:@"nom_programme"]; 


    return cell; 


} 

回答

1

您需要重新加載UITableView一旦你的異步請求有完成。目前,您將UIViewController推入堆棧,稍後將設置它的數據源,因此只有在滾動時纔會加載該單元格。

爲了解決這個問題,你設置cd.categcd.categArray後,請確保你要麼打電話[cd.tableView reloadData],或在setter方法categcategArray呼叫[self.tableView reloadData]

1

太晚了,但我認爲你要處理的數據是顯示在錯誤的地方。一段時間的用戶體驗是看到沒有數據的空白屏幕。

將所有數據移動到被推送的控制器中會更好。因此,在您的選擇中,創建控制器並將要提取的數據設置爲屬性。在ProgViewController中,當viewDidAppear內部開始提取並激活一個活動指示器顯示它正忙。成功或失敗時停止活動指示器。成功時,調用self.tableView.reloadData。失敗時,提高警報並彈出控制器。

通過這種方式,一切都被封裝起來並在正確的位置處理,隨着UI更新其內部數據狀態的變化。

如果你從初始控制器內部進行抓取,那麼你應該顯示它忙,只有當你有一個結果顯示恕我直言,才推新控制器。

+1

+1有效點,這將是一個更好的代碼結構方法。我只是回答提出的問題。 – Tim 2015-02-24 11:54:09