Iphone 通过选择UITableView行调用新视图时出错
根据在UITableView中选择的行,我在调出特定的ViewController时遇到一些问题。我有一个表,它包含3个部分,每个部分有4行 当选择一行时,我将其设置为为为每行调用一个新视图,因此根据选择的行,总共需要调用12个视图。问题是,只调用前4个视图,当我点击第5行时,它会显示一个新视图,但它会调出第一个视图 我已使用以下代码设置UITableViewIphone 通过选择UITableView行调用新视图时出错,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,根据在UITableView中选择的行,我在调出特定的ViewController时遇到一些问题。我有一个表,它包含3个部分,每个部分有4行 当选择一行时,我将其设置为为为每行调用一个新视图,因此根据选择的行,总共需要调用12个视图。问题是,只调用前4个视图,当我点击第5行时,它会显示一个新视图,但它会调出第一个视图 我已使用以下代码设置UITableView carbData = [[NSArray alloc] initWithObjects:@"What Are Carbs?",@"Why
carbData = [[NSArray alloc] initWithObjects:@"What Are Carbs?",@"Why Do I Need Carbs?",@"Simple Vs. Complex",@"Fun Five Facts",nil];
proteinData = [[NSArray alloc] initWithObjects:@"What is Protein?",@"Why Do I Need Protein?",@"Complete Vs. Incomplete",@"Fun Five Facts",nil];
fatData = [[NSArray alloc] initWithObjects:@"What Are Fats?",@"Why Do I Need Fats?",@"Saturated Vs. Unsaturated",@"Fun Five Facts",nil];
[self setTitle:@"Nutrition Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.carbData count];
return [self.proteinData count];
return [self.fatData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [carbData objectAtIndex:indexPath.row];
if(indexPath.section == 1)
cell.text = [proteinData objectAtIndex:indexPath.row];
if (indexPath.section == 2)
cell.text = [fatData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0)
return @"Carbohydrates";
if (section ==1)
return @"Protein";
else if (section ==2)
return @"Fats";
}
然后,当选择某一行时,我使用以下代码确定要调用哪个视图
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[carbData objectAtIndex:indexPath.row] isEqual:@"What Are Carbs?"]) {
CarbsView1 *controller = [[CarbsView1 alloc] initWithNibName:@"CarbsView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Why Do I Need Carbs?"]) {
CarbsView2 *controller = [[CarbsView2 alloc] initWithNibName:@"CarbsView2" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Simple Vs. Complex"]) {
CarbsView3 *controller = [[CarbsView3 alloc] initWithNibName:@"CarbsView3" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Fun Five Facts"]) {
CarbsView4 *controller = [[CarbsView4 alloc] initWithNibName:@"CarbsView4" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[proteinData objectAtIndex:indexPath.row] isEqual:@"What is Protein?"]) {
ProteinView1 *controller = [[CarbsView4 alloc] initWithNibName:@"ProteinView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
该应用程序编译和构建得很好,一切都按照我的意愿运行,但是当我查看使用的标题时,当我尝试并选择“What is Protein?”时,会在第一条if语句中调出“What is Carbs?”的视图
任何帮助都将不胜感激“什么是蛋白质?”见第1节第0行。“什么是碳水化合物?”在第0节第0行。请注意,您的didSelectRowAtIndexPath方法不会在任何地方检查节号。因此,当点击第1节第0行时,第一条if语句返回true。我想你想要的东西更像:
if (indexPath.section == 0) {
if(indexPath.row == 0) {
//present appropriate view controller
} else if (indexPath.row == 1) {
} else if (indexPath.row == 2) {
} ...
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
} else if (indexPath.row == 1) {
} ...
} ...
你就在这一秒丰富了我的生活。这非常有效,我知道是这样的,但我不知道如何实施规则。多谢各位