草庐IT

ios - 静态单元格内的动态 UITableView

coder 2024-01-10 原文

我已经阅读了一些关于静态和动态单元格不兼容的帖子,但我想知道是否有适合我的情况的解决方法。

我有一个静态表(由 UITableViewController 处理)。我在其中一个单元格中放置了一个动态表。委托(delegate)和数据源是两个表的 UITableViewController,只要内部动态表的行数少于静态表,它就可以很好地工作。当动态表的单元格多于静态表时,我得到一个 index i beyond bounds 异常。

我假设以某种方式静态定义单元格总数并由两个表共享,但无法真正理解到底发生了什么。有人遇到过类似的问题并找到了解决方法吗?

编辑

这是我的 numberOfRowsInSection 方法。在我的委托(delegate)/数据源的每个方法中,我检查调用表是动态表 (_tableOptions) 还是静态表(在这种情况下,我调用父方法)。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _tableOptions) {
        // I return here the number of rows for the dynamic inner table
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableOptions) {
        static NSString *simpleTableIdentifier = @"cellOptions";

        CellOptions *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        // Doing some stuff with my cell... 

        return cell;

    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

最佳答案

这不是兼容性问题。如果您选择为静态 UITableView 实现编程控制功能,那么您必须确保您不会与它们在 Storyboard中的定义方式发生冲突。

例如,如果您为具有静态单元格的 UITableView 实现此功能

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return 5;
}

但是您只为 Storyboard 中的给定 View 和 Controller 添加了 3 个静态单元格,那么 Xcode 不能简单地从无到有再创建 2 个静态单元格。我认为最接近解决方法的是为带有动态单元格的表添加另一个 UITableViewController 。您可以在您正在使用的当前 Controller 中实例化新 Controller ,而无需在屏幕上显示其 View 。然后,您可以将具有动态单元格的 UITableView 指定为新的 UITableViewController 的 tableView 属性。同样,将 UITableView 的委托(delegate)和数据源属性指定为新 Controller 。

编辑:在看到代码后,我知道了一种可能的解决方法。您可以利用部分的数量来欺骗 UITableViewController 执行您想要的操作。

您可以使用下面的代码将任意数量的单元格添加到动态 View 中,因为您将单元格添加到动态表的第二部分,但同时将静态表第二部分中的单元格数设置为 0 . 关键是您必须 将最大数量的单元格添加到 Storyboard中静态表的第二部分。这些将是永远不会显示的虚拟单元格。

在下图中,您可以看到我将静态表格的第二部分设置为 10 个单元格,并且在代码中我能够为 dynamic tableview 返回最多 10 个单元格。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == dynamic)
    {
        return 2;
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == dynamic)
    {
        if (section == 1)
        {
            return 10;
        }
    }
    else
    {
        if (section == 0)
        {
            return [super tableView:tableView numberOfRowsInSection:section];
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == dynamic) {
        static NSString *simpleTableIdentifier = @"sample";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        [cell.textLabel setText:[NSString stringWithFormat:@"cell %d",indexPath.row]];

        // Doing some stuff with my cell...

        return cell;
    }
    else
    {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

您可以通过实现此功能来清除节标题:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

删除部分的标题后,您将得到您想要完成的目标。在静态表的第三个单元格内的动态表中有 10 个单元格。

关于ios - 静态单元格内的动态 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777878/

有关ios - 静态单元格内的动态 UITableView的更多相关文章

  1. 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返回它复制的字节数,但是当我还没有下

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

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

  3. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  4. 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使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby - 在 Ruby 中动态创建数组 - 2

    有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.

  6. ruby - 单元测试文件 I/O 方法 - 2

    我对单元测试还是比较陌生。我用Ruby编写了一个类,它接受一个文件,在该文件中搜索给定的Regex模式,替换它,然后将更改保存回文件。我希望能够为此方法编写单元测试,但我不知道我将如何去做。有人能告诉我我们如何对处理文件i/o的方法进行单元测试吗? 最佳答案 看看这个HowdoIunit-testsavingfiletothedisk?基本上这个想法是一样的,文件系统是你的类的依赖。所以引入一个可以在你的单元测试中模拟的角色/接口(interface)(这样你在单元测试时就没有依赖性);角色中的方法应该是您从文件系统中需要的所有东西

  7. ruby - 是否可以将 IRB 提示配置为动态更改? - 2

    我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO

  8. 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上

  9. ruby-on-rails - carrierwave:在序列化动态属性上安装 uploader - 2

    首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟

  10. ruby - 尝试运行 minitest 单元测试时出错 - 2

    尝试使用rubytest/test_foo.rb运行minitest单元测试时出现以下错误:Warning:youshouldrequire'minitest/autorun'instead.Warning:oradd'gem"minitest"'before'require"minitest/autorun"'From:/home/emile/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/autorun.rb:15:```test_foo.rb看起来像这样:require'minitest/autorun'classTestFoo

随机推荐