我无法填充第二个UITableView控制器,想知道是否有人可以提供帮助。无法填充第二个UITableView控制器
我正在使用网站API,JSON和RestKit作为数据。我相信这部分工作正常,因为我的第一个VC加载正常。
但我不确定是否需要使用prepareForSegue和/或didSelectRowAtIndexPath,以便可以识别第一个VC中选定的单元格/行,以便第二个VC填充(正确)数据。
1 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,让之前的应用程序崩溃到第二个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.count'和'league.teams.count'。 – HRM
@Hari我不完全确定这是你的意思,但是当我选择联盟“NBA”单元格/行时,我记录了你在第一个VC上的建议。并且它记录了正确的league.name的“全国篮球协会”,但它记录了leagues.count和league.teams.count的“0”(这对于league.teams.count是有意义的,因为球队将在第二VC)。我无法在第二个VC上尝试任何NSLog,因为到目前为止,我所能得到的只是一张空白表或者应用程序崩溃。这些帮助有用?欣赏响应很多! – Realinstomp
因此,如果leagues.count为0,那么该表如何填充第二个vc?你在numberOfSectionsInTableView中返回leagues.count作为第二个vc,rt ?.另外,我认为你应该在第二个vc中设置'leagues',从didSelectRowAtIndexPath的第一个vc开始。比如'teamsViewController.leagues' =选中的联赛数据。 – HRM