根据我在UITableView中选择的行,我在提取特定的ViewControllers方面遇到了一些问题。我有一个表,其中包含3个部分,每个部分有4行。
当选择一行时,我将其设置为每行调用一个新视图,因此根据选择的行调用总共12个视图。问题是,只调用前4个视图,当我点击第5行时,它会显示一个新视图,但它会拉出第一个视图。
我已使用以下代码设置了我的UITableView;
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];
}
应用程序编译和构建正常,一切都按照我的喜好工作,但是当我查看使用的标题时,当我尝试选择“什么是蛋白质?”时拉起了“什么是碳水化合物?”的观点。这是第一个if语句。
任何帮助都会受到大力赞赏
答案 0 :(得分:1)
“什么是蛋白质?”在第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) {
} ...
} ...