2013-01-20 24 views
1

我無法填充第二個UITableView控制器,想知道是否有人可以提供幫助。無法填充第二個UITableView控制器

我正在使用網站API,JSON和RestKit作爲數據。我相信這部分工作正常,因爲我的第一個VC加載正常。

但我不確定是否需要使用prepareForSegue和/或didSelectRowAtIndexPath,以便可以識別第一個VC中選定的單元格/行,以便第二個VC填充(正確)數據。

1 VC填入正確的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return sports.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    Sport *sport = [sports objectAtIndex:section]; 
    return sport.leagues.count; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    Sport *sport = [sports objectAtIndex:section]; 
    return sport.name; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"standardCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    Sport *sport = [sports objectAtIndex:indexPath.section]; 
    League *league = [sport.leagues objectAtIndex:indexPath.row]; 
    cell.textLabel.text = league.name; 

    return cell; 
} 

第二VC填充空白表:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return leagues.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    League *league = [leagues objectAtIndex:section]; 
    return league.teams.count; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    League *league = [leagues objectAtIndex:section]; 
    return league.name; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"standardCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    League *league = [leagues objectAtIndex:indexPath.section]; 
    Team *team = [league.teams objectAtIndex:indexPath.row]; 
    cell.textLabel.text = team.name; 

    return cell; 
} 

或者,如果我嘗試添加額外的代碼爲1 VC,讓之前的應用程序崩潰到第二個VC:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TeamsViewController *teamsViewController = [[TeamsViewController alloc] initWithNibName:@"TeamsViewController" bundle:nil]; 
    teamsViewController.title = [[sports objectAtIndex:indexPath.row] objectForKey:@"sports"]; 

} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"leagueDetail"]) { 
     TeamsViewController *tvc = [segue destinationViewController]; 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     tvc.data = [self.navigationController objectInListAtIndex:indexPath.row] 
} 

真的感謝任何幫助!

+0

在你的第二個視圖控制器中,你在哪裏設置'leaugues'?嘗試記錄你的'leagues.count'和'league.teams.count'。 – HRM

+0

@Hari我不完全確定這是你的意思,但是當我選擇聯盟「NBA」單元格/行時,我記錄了你在第一個VC上的建議。並且它記錄了正確的league.name的「全國籃球協會」,但它記錄了leagues.count和league.teams.count的「0」(這對於league.teams.count是有意義的,因爲球隊將在第二VC)。我無法在第二個VC上嘗試任何NSLog,因爲到目前爲止,我所能得到的只是一張空白表或者應用程序崩潰。這些幫助有用?欣賞響應很多! – Realinstomp

+0

因此,如果leagues.count爲0,那麼該表如何填充第二個vc?你在numberOfSectionsInTableView中返回leagues.count作爲第二個vc,rt ?.另外,我認爲你應該在第二個vc中設置'leagues',從didSelectRowAtIndexPath的第一個vc開始。比如'teamsViewController.leagues' =選中的聯賽數據。 – HRM

回答

1

我有點困惑,除了創建UITableViewCell對象。當你問表單視圖來取出單元格時,它會返回一個當前不需要的單元格,如果沒有未使用的單元格,你必須創建一個新單元格。試試這樣的代碼:

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

    // Create cell 
    NSString *cellIdentifier = @"cell"; 
    UITableViewCell *cell = nil; 
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"cell %d", indexPath.row]; 

    return cell; 
} 
+0

感謝您的迴應!這是第一個VC還是第二個VC?這就是這樣的應用程序不會崩潰在我身上?或者,這也有助於將數據從第一個VC傳遞到第二個VC,我認爲這是另一個可能發生的問題。 – Realinstomp

+0

上面的代碼片段是我通常在我的VC中使用的代碼,所以我的意思是你應該在VC中使用它。 –

+0

聽起來不錯,我會把它放在那裏 – Realinstomp

相關問題