0
votes

I'm using a website API to populate my tableViews, using Restkit and JSON for request/receive/parse data into my model. Already set up models and mapping which seem to be working fine.

The problem is after the initial table view that loads Section & Row, I need the second tableView to use the cell/row selected from the tableView, to show the team associated with that specific League that was chosen in the second tableView.

I believe it's the ...

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

that I don't have or have correct, because it's:

Populating data correctly in initial screen:

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

But if I select a cell, the second ViewController is a blank tableView (note: no prepareForSegue or didSelectRowAtIndexPath code in previous ViewController tableView code, so I think thats why I might be getting this blank 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;
}

Or if I add prepareForSegue and didSelectRowAtIndexPath code in previous ViewController, select a cell which would move us to the next ViewController, the app crashes (prepareForSegue and didSelectRowAtIndexPath code aren't correct for sure, I was trying to just piece something together even though I did at least some of it is wrong):

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

I think what I'm running into is needing for the selected cell to pass its info to the next tableView somehow, and I don't know if it needs to somehow append that info to the baseURL I'm retrieving the data from for that selected row, and then also somehow show the data in the teams tableView. But I'm not sure if thats the problem, and/or how exactly to implement it.

Would appreciate any help, and just let me know if more code/details are needed, I'd be happy to get them quickly for anyone.

EDIT: Here is my updated prepareForSegue code, still have error though- Error: Property 'leagues' not found on object of type '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];
    }
}
1
Amazing! It crashes without producing any sort of diagnostic message!Hot Licks

1 Answers

2
votes

You need to link your Prototype Row in storyboard with the next view controller, name the segue and use prepareForSegue to pass the necessary data. No need for didSelectRow....

To reference the row, use

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

(I would not rely on the selected state of the row. You would be mixing View and Model domains, violating MVC principles.)

Also, I noticed a certain logic mistake. If in VC2 you want to show leagues as the sections and teams on the table view rows, you should really be selecting a Sport not a league. A league does not have many leagues, right?