Realinstomp
Realinstomp

Reputation: 532

Not able to populate second UITableView Controller

I'm unable to populate the second UITableView Controller, wondering if anyone could help.

I'm using a websites API, JSON, and RestKit for the data. I believe that part is working fine because my first VC loads fine.

But I'm not sure if I need to use prepareForSegue and/or didSelectRowAtIndexPath so that I can identify the cell/row selected in the first VC, so that the second VC populates with the (correct) data.

1st VC populates correct:

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

2nd VC populates blank table:

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

Or if I try to add extra code for 1st VC, app crashes before getting to 2nd 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]
}

Would really appreciate any help!

Upvotes: 1

Views: 244

Answers (1)

Rostyslav Druzhchenko
Rostyslav Druzhchenko

Reputation: 3773

I am a little bit confused with excepting of creation UITableViewCell object. When you ask table view for dequeuing cell it returns one which it does not need at the moment, if there are no unused cell you have to create a new one. Try code like this:

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

Upvotes: 1

Related Questions