我無法填充第二個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