【发布时间】:2013-01-20 20:45:18
【问题描述】:
我无法填充第二个 UITableView 控制器,想知道是否有人可以提供帮助。
我正在使用网站 API、JSON 和 RestKit 来获取数据。我相信这部分工作正常,因为我的第一个 VC 加载正常。
但我不确定是否需要使用 prepareForSegue 和/或 didSelectRowAtIndexPath 以便我可以识别在第一个 VC 中选择的单元格/行,以便第二个 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;
}
第二个 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 添加额外代码,应用程序会在到达第 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]
}
会真的感谢任何帮助!
【问题讨论】:
-
在你的第二个视图控制器中,你在哪里设置
leaugues?尝试记录您的leagues.count和league.teams.count。 -
@Hari 我不完全确定这是否是你的意思,但是当我选择联盟“NBA”单元格/行时,我 NSLog'd 你在第一个 VC 上的建议。它记录了正确的联赛名称“国家篮球协会”,但它记录了 Leagues.count 和 League.teams.count 的“0”(这对 League.teams.count 有意义,因为球队将在第二个VC)。我无法在第二个 VC 上尝试任何 NSLog,因为到目前为止,我所能得到的只是一张空白表或此时应用程序崩溃。这些帮助有用?非常感谢您的回复!
-
如果leagues.count 为0,那么第二个vc 上的表格是如何填充的?您将在 numberOfSectionsInTableView 中为第二个 vc,rt 返回leagues.count?另外,我认为您应该在 didSelectRowAtIndexPath 上的第一个 vc 的第二个 vc 中设置
leagues。喜欢teamsViewController.leagues= 选定的联赛数据。 -
@Hari 该表未在第二个 VC 上填充。这就是我遇到的问题
标签: ios objective-c json uitableview uiviewcontroller