1

2 番目の UITableView コントローラーを設定できません。誰か助けてくれるかどうか疑問に思っています。

データには Web サイト API、JSON、および RestKit を使用しています。最初の VC が正常にロードされるため、その部分は正常に機能していると思います。

しかし、最初の VC で選択されたセル/行を識別できるように prepareForSegue や didSelectRowAtIndexPath を使用する必要があるかどうかはわかりません。これにより、2 番目の 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;
}

2 番目の 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;
}

または、最初の 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]
}

本当に助けていただければ幸いです!

4

1 に答える 1

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;
}
于 2013-01-20T21:16:36.560 に答える