2013-01-20 21 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

相关问题