0

我正在使用网站 API 来填充我的 tableViews,使用 Restkit 和 JSON 将数据请求/接收/解析到我的模型中。已经设置了似乎工作正常的模型和映射。

问题是在加载 Section & Row 的初始表视图之后,我需要第二个 tableView 使用从 tableView 中选择的单元格/行,以显示与在第二个 tableView 中选择的特定联赛相关联的团队。

我相信这是...

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

我没有或没有正确的,因为它是:

在初始屏幕中正确填充数据:

- (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;
}

但是如果我选择一个单元格,第二个 ViewController 是一个空白的 tableView(注意:之前的 ViewController tableView 代码中没有 prepareForSegue 或 didSelectRowAtIndexPath 代码,所以我认为这就是为什么我可能会得到这个空白的 tableview):

- (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;
}

或者,如果我在前一个 ViewController 中添加 prepareForSegue 和 didSelectRowAtIndexPath 代码,选择一个单元格将我们移动到下一个 ViewController,应用程序崩溃(prepareForSegue 和 didSelectRowAtIndexPath 代码肯定不正确,我试图只是拼凑一些东西,即使我至少其中一些是错误的):

-(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]
}

我认为我遇到的问题是所选单元格需要以某种方式将其信息传递给下一个 tableView,我不知道它是否需要以某种方式将该信息附加到我从中检索数据的 baseURL选定的行,然后还以某种方式显示团队 tableView 中的数据。但我不确定这是否是问题所在,和/或如何具体实施。

非常感谢任何帮助,如果需要更多代码/详细信息,请告诉我,我很乐意为任何人快速获取它们。

编辑:这是我更新的 prepareForSegue 代码,但仍然有错误 - 错误:在“TeamsViewController”类型的对象上找不到属性“联盟”

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"leagueDetail"]) {

        // note that "sender" will be the tableView cell that was selected
        UITableViewCell *cell = (UITableViewCell*)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        TeamsViewController *tvc = (TeamsViewController *)[segue destinationViewController];
        tvc.leagues = [leagues objectAtIndex:indexPath.section];
    }
}
4

1 回答 1

2

您需要将情节提要中的Prototype Row与下一个视图控制器链接,命名 segue 并用于prepareForSegue传递必要的数据。不需要didSelectRow...

要引用该行,请使用

[self.tableView indexPathForCell:(UITableViewCell*)sender]

(我不会依赖行的选定状态。您将混合视图和模型域,违反 MVC 原则。)

另外,我注意到某个逻辑错误。如果在 VC2 中您想将联赛显示为表格视图行上的部分和球队,那么您真的应该选择一项运动而不是联赛。一个联赛没有多少联赛,对吧?

于 2013-01-21T12:48:07.033 回答