无法填充第二个 UITableView 控制器

Posted

技术标签:

【中文标题】无法填充第二个 UITableView 控制器【英文标题】:Not able to populate second UITableView Controller 【发布时间】:2013-01-20 20:45:18 【问题描述】:

我,想知道是否有人可以提供帮助。

我正在使用网站 API、JSON 和 RestKit 来获取数据。我相信这部分工作正常,因为我的第一个 VC 加载正常。

但我不确定是否需要使用 prepareForSegue 和/或 didSelectRowAtIndexPath 以便我可以识别在第一个 VC 中选择的单元格/行,以便第二个 VC 填充(正确的)数据。

第一个 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 添加额外代码,应用程序会在到达第 2 个 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]

真的感谢任何帮助!

【问题讨论】:

在你的第二个视图控制器中,你在哪里设置leaugues?尝试记录您的 leagues.countleague.teams.count @Hari 我不完全确定这是否是你的意思,但是当我选择联盟“NBA”单元格/行时,我 NSLog'd 你在第一个 VC 上的建议。它记录了正确的联赛名称“国家篮球协会”,但它记录了 Leagues.count 和 League.teams.count 的“0”(这对 League.teams.count 有意义,因为球队将在第二个VC)。我无法在第二个 VC 上尝试任何 NSLog,因为到目前为止,我所能得到的只是一张空白表或此时应用程序崩溃。这些帮助有用?非常感谢您的回复! 如果leagues.count 为0,那么第二个vc 上的表格是如何填充的?您将在 numberOfSectionsInTableView 中为第二个 vc,rt 返回leagues.count?另外,我认为您应该在 didSelectRowAtIndexPath 上的第一个 vc 的第二个 vc 中设置 leagues。喜欢teamsViewController.leagues = 选定的联赛数据。 @Hari 该表未在第二个 VC 上填充。这就是我遇到的问题 【参考方案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;

【讨论】:

感谢您的回复!是第一个VC还是第二个VC?那是为了让应用程序不会在我身上崩溃吗?或者这也有助于将数据从第一个 VC 传递到第二个 VC,我假设这是可能发生的另一个问题。 上面的代码片段是我通常在我的 VC 中使用的代码,所以我的意思是你应该在你的两个 VC 中都使用它。

以上是关于无法填充第二个 UITableView 控制器的主要内容,如果未能解决你的问题,请参考以下文章

单个接口控制器中的两个表在 WatchKit 中不起作用

无论如何用数组填充 UITableView 但保留复选标记?

具有多个视图控制器的 UITableView

如何使用 didSelectRowAtIndexPath 将变量从 uitableview 传递到第二个 tableview?

加载 UItableView 以响应 uitextfield

uitableview didselectrowatindex 仅适用于第一个表视图而不适用于同一视图控制器中的第二个表视图?