草庐IT

ios - Table View Controller 每行连接到不同的 View Controller

coder 2023-09-25 原文

我正在尝试开发一个 TableView Controller ,其中行连接到多个 View Controllers(TextField、TextView、TableView、DatePicker、ImageView 等)。

所以如果我点击任何一行,它应该打开 Intermediate View 并将适当的 Controller 放在一个公共(public)位置,所有 Controller 的其余部分都是相同的。假设我点击了一个索引映射到 TableView 的行。当它打开中间 Controller 时,它应该将 tableview 放在公共(public)容器中,这个 table view 应该来自所有其他 Tableview 的单个 TableView Controller 。

我是 ios 的新手,不会设计这个。

设计这个的最佳方法是什么?我该如何实现?

谢谢

最佳答案

我建议不要在 Storyboard 中创建单元格并连接它。而是在 Storyboard 中保留空表并使用代码创建单元格。您可以通过继承 UITableViewCell 来创建自定义单元格。

在 Storyboard 中,您只需使用 segue 将 TableView 与所有 View Controller 链接起来,并为其指定正确的标识符名称

现在实现UITableView 的所有委托(delegate)方法。覆盖 -tableView:didSelectRowAtIndexPath: 方法并在行选择上对特定行执行 segue。

示例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"BasicCoreDataSegue" sender:self];
            break;

        default:
            break;
    }
}

在上面的例子中,如果您选择第一行,它将推送与 Storyboard 中的 BasicCoreDataSegue segue 连接的 View Controller ,您可以将其与图像进行比较。

使用类似的方式创建其他 segues 并在不同 switch case 的 didSelectRowAtIndexPath 方法中调用它们。

此外,如果您想将任何值传递给推送 Controller ,请覆盖以下方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([[segue identifier] isEqualToString:@"BasicCoreDataSegue"]) {
        // Get reference to the destination view controller
        TextViewController *vc = [segue destinationViewController];
        vc.textView.text = "Hello";
    }
} 

编辑:

以上代码适用于普通 Controller 。现在您不需要在 didSelectRowAtIndexPath 方法中创建更多的 segues set Intermediate controller segue。

使用[self.tableView indexPathForSelectedRow] 方法在prepareForSegue 方法中获取选定的行。

例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self performSegueWithIdentifier:@"BasicCoreDataSegue" sender:self];
}

现在,当 prepareForSegue 被调用时,然后为中间 Controller 设置整数值。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].

        // You can get selected row using below line
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        // Pass the selected object to the new view controller.
        if ([[segue identifier] isEqualToString:@"BasicCoreDataSegue"]) {
            // Get reference to the destination view controller
            IntermediateController *vc = [segue destinationViewController];
            vc.selectedIndex = indexPath.row;
        }
    }

在上面的代码中,selectedIndex 是一个整数变量,用于跟踪选择了哪一行。

现在在 -viewDidLoad() 的中间 Controller 中使用 switch case 从行选择中获取您想要的 Controller 对象,并将其 View 作为 subview 添加到中间 Controller 中。

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

TextViewController *controller = (TextViewController*)[storyBoard 
                    instantiateViewControllerWithIdentifier: @"TextViewControllerId"];

[self.topView addSubview:controller.view];

关于ios - Table View Controller 每行连接到不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27775630/

有关ios - Table View Controller 每行连接到不同的 View Controller的更多相关文章

  1. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  2. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  7. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

  8. ruby - 我的 Ruby IRC 机器人没有连接到 IRC 服务器。我究竟做错了什么? - 2

    require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame

  9. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  10. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

随机推荐