【问题标题】:Displaying content on new view depending on didSelectRowAtIndexPath根据 didSelectRowAtIndexPath 在新视图上显示内容
【发布时间】:2012-01-21 03:49:35
【问题描述】:

我正在制作一个简单的应用程序,试图让自己熟悉情节提要及其用法。该应用程序加载了一个初始表视图,该表视图通过我的 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
}

我认为这完全是错误的做法,因为它需要大量开销以及创建我需要的许多视图的 nib。有人告诉我,我应该只添加一个视图,然后根据选择的行将相关图像传递给该视图。

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

【问题讨论】:

    标签: iphone objective-c xcode uitableview storyboard


    【解决方案1】:

    假设您有一个数组,其中包含您在表格中显示的元素。您可能有一个包含这样的字符串项的数组(称为 myArray)... 第 1 项,第 2 项,第 3 项,第 4 项

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

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

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

    现在在您的 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];
    

    注意:我意识到 controller.graphic 不存在 - 我只是将其放入以向您展示设置图像的位置。您实际上如何做到这一点取决于您的其他对象和控制器,但希望这会给您一个想法。其他代码 sn-ps 也是如此。

    【讨论】:

      【解决方案2】:
      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];
       }
      

      这样做...

      【讨论】:

      • 那是一个标签。我想在 UIImageView 中传递图像。
      • 保罗也一样,controller.second.image=firstview.image
      • 不,它不起作用。这可能是我的问题不够清楚。
      • 必须在第二个视图中声明 UIImageView 并将 secondViewController.h 添加到第一个视图中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多