根据didSelectRowAtIndexPath在新视图上显示内容

Pau*_*ris 0 iphone xcode objective-c storyboard uitableview

我正在做一个简单的应用程序,试图熟悉故事板及其使用.应用程序加载初始表视图,该视图通过我的AppDelegate.m didFinishLaunchingWithOptions方法填充.这工作正常,所以没有问题.

下一个阶段取决于在我的表格中点击哪个单元格我希望显示具有不同图像的视图.在以前的生活中,我会为每个人创建一个新的.xib文件,最终可能会有100个.xib文件,每个文件都有不同的图像.然后我会使用以下代码来显示该视图;

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
}
Run Code Online (Sandbox Code Playgroud)

我想这完全是错误的做法,因为它需要大量的开销和我需要的许多视图的创建笔尖.我被告知我应该只有一个额外的视图,并根据选择的行将相关图像传递给该视图.

我不知道怎么做.我在故事板中创建了一个额外的视图控制器,它链接到表视图.我将如何根据表格中的哪一行选择传递不同的图像?

Nic*_*ull 6

假设您有一个数组,其中包含您在表中显示的元素.你可能有一个数组(称为myArray),其中包含像这样的字符串项... item1,item2,item3,item4

目前,这些只是在您的tableview上显示.

现在,如果您改为创建一个对象(或结构)数组,其中包含显示名称和要显示的图形的名称

(item1,graphic1),(item2,graphic2),(item3,graphic3),(item4,graphic4)

现在,在didSelectRow方法中,您可以获取他们选择的项目

selectedItem = [myArray objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)

从中你可以得到图形的名称

selectedGraphic = selectedItem.graphicName;
Run Code Online (Sandbox Code Playgroud)

现在,您可以创建新视图并为其提供图形

UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Run Code Online (Sandbox Code Playgroud)

注意:我意识到controller.graphic不存在 - 我只是把它放在显示你设置图像的位置.你如何实际做到这一点取决于你的其他对象和控制器,但希望这会给你一个想法.其他代码片段也是如此.