Iphone 根据didSelectRowAtIndexPath在新视图上显示内容
我正在做一个简单的应用程序,试图让自己熟悉故事板及其使用。应用程序加载初始表视图,该视图通过my AppDelegate.m didFinishLaunchingWithOptions方法填充。这很好,所以没有问题 下一步取决于在我的表格中点击哪个单元格,我希望显示一个具有不同图像的视图。在以前的生活中,我会为所有这些文件创建一个新的.xib文件,最终可能会有100个.xib文件,每个文件上都有不同的图像。然后,我会使用以下代码来显示该视图Iphone 根据didSelectRowAtIndexPath在新视图上显示内容,iphone,objective-c,xcode,uitableview,storyboard,Iphone,Objective C,Xcode,Uitableview,Storyboard,我正在做一个简单的应用程序,试图让自己熟悉故事板及其使用。应用程序加载初始表视图,该视图通过my AppDelegate.m didFinishLaunchingWithOptions方法填充。这很好,所以没有问题 下一步取决于在我的表格中点击哪个单元格,我希望显示一个具有不同图像的视图。在以前的生活中,我会为所有这些文件创建一个新的.xib文件,最终可能会有100个.xib文件,每个文件上都有不同的图像。然后,我会使用以下代码来显示该视图 if (indexPath.row == 0) {
if (indexPath.row == 0)
{
UIViewController *controller = [[UIViewController alloc]initWithNibName:@"AlertViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if (indexPath.row == 1)
{
// Show a different view
}
我认为这是完全错误的做法,因为它需要大量的开销和创建我所需要的任意多个视图的笔尖。我被告知,我应该只拥有一个附加视图,并根据选择的行将相关图像传递给该视图
但是我不知道怎么做。我在我的故事板中创建了一个附加的视图控制器,它链接到表视图。根据表中选择的行,如何传入不同的图像
if (indexPath.row == 0)
{
UIViewController *controller = [[UIViewController alloc]initWithNibName:@"AlertViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.mylable1.text=@"xyz";
controller.mylable2.text=@"abc";
.
.
.
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
像这样做
这样做…假设您有一个数组,其中包含要在表中显示的元素。您可能有一个数组(称为myArray),其中包含如下字符串项。。。 第1项、第2项、第3项、第4项 目前,这些仅显示在tableview上 现在,如果您改为创建一个对象(或结构)数组,其中包含显示名称和要显示的图形名称 (第1项,图形1)、(第2项,图形2)、(第3项,图形3)、(第4项,图形4) 现在,在didSelectRow方法中,您可以获得他们选择的项目
selectedItem = [myArray objectAtIndex:indexPath.row];
从中可以得到图形的名称
selectedGraphic = selectedItem.graphicName;
UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
现在,您可以创建新视图并为其提供图形
selectedGraphic = selectedItem.graphicName;
UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
注意:我意识到controller.graphic并不存在——我只是把它放进去,告诉你在哪里设置图像。您实际如何执行此操作取决于您的其他对象和控制器,但希望这能给您一个想法。其他代码片段也是如此。假设您有一个数组,其中包含要在表中显示的元素。您可能有一个数组(称为myArray),其中包含如下字符串项。。。 第1项、第2项、第3项、第4项 目前,这些仅显示在tableview上 现在,如果您改为创建一个对象(或结构)数组,其中包含显示名称和要显示的图形名称 (第1项,图形1)、(第2项,图形2)、(第3项,图形3)、(第4项,图形4) 现在,在didSelectRow方法中,您可以获得他们选择的项目
selectedItem = [myArray objectAtIndex:indexPath.row];
从中可以得到图形的名称
selectedGraphic = selectedItem.graphicName;
UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
现在,您可以创建新视图并为其提供图形
selectedGraphic = selectedItem.graphicName;
UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
注意:我意识到controller.graphic并不存在——我只是把它放进去,告诉你在哪里设置图像。您实际如何执行此操作取决于您的其他对象和控制器,但希望这能给您一个想法。其他代码片段也是如此。这是一个标签。我想在UIImageView中传入一个图像。同样的,Paul,controller.second.image=firstview.imageNo,它不工作。这可能是因为我的问题不够清楚。您必须在第二个视图中声明UIImageView,并将secondViewController.h添加到第一个视图中,这是一个标签。我想在UIImageView中传入一个图像。同样的,Paul,controller.second.image=firstview.imageNo,它不工作。可能是我的问题不够清楚。您必须在第二个视图中声明UIImageView,并在第一个视图中添加secondViewController.h