gpt4 book ai didi

iphone - 通过选择 UITableView 行调用新 View 错误

转载 作者:行者123 更新时间:2023-11-29 05:05:01 25 4
gpt4 key购买 nike

根据 UITableView 中选择的行,我在拉取特定 ViewController 时遇到一些问题。我有一个表,其中包含 3 个部分,每个部分有 4 行。

当选择一行时,我将其设置为为每行调用一个新 View ,因此根据选择的行总共调用 12 个 View 。问题是,只有前 4 个 View 被调用,当我点击第 5 行时,它会显示一个新 View ,但会拉出第一个 View 。

我已经使用以下代码设置了我的 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";
}

然后,我使用以下代码来确定当选择某一行时要调用哪个 View ;

-(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];
}

该应用程序编译和构建得很好,一切都按照我的意愿运行,但是当我查看使用的标题时,当我尝试选择“什么是蛋白质?”时是拉出“什么是碳水化合物?”的 View 。这是在第一个 if 语句中。

任何帮助将不胜感激

最佳答案

“什么是蛋白质?”位于第 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) {

} ...
} ...

关于iphone - 通过选择 UITableView 行调用新 View 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5750858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com