【问题标题】:Dynamic UITableView inside static cell静态单元格内的动态 UITableView
【发布时间】:2013-12-26 00:23:45
【问题描述】:

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

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

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

编辑

这是我的numberOfRowsInSection 方法。在我的委托/数据源的每个方法中,我检查调用表是动态表 (_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];
    }
}

【问题讨论】:

  • 您能否向我们展示有关数据源的代码。
  • 你能把你写在cell配置方法中的代码粘贴过来吗?
  • 感谢@KudoCC,我放了一些代码,希望能说明我如何处理指向同一个控制器的两个表。
  • 我需要- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 实现和超出范围的数组的详细信息。
  • @KudoCC 刚刚更新了我的答案。我无法控制导致问题的数组,我猜这是UITableViewController 用来存储静态视单元的数组。

标签: ios objective-c uitableview


【解决方案1】:

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

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

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

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

编辑: 看到代码后,我知道一种可能的解决方法。你可以利用节的数量来欺骗 UITableViewController 做你想做的事。

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

在下图中,您可以看到我将静态表格的第二部分设置为 10 个单元格,并且在代码中我最多可以为 dynamic 表格视图返回 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;
}

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

【讨论】:

  • 谢谢@Mike。实际上,我返回不同的值取决于哪个表是tableView。我想每个表都有不同的数据源/委托应该可以工作,但仍然不明白为什么我不能对两个表使用一个控制器。
  • 哦,这很有趣。看到代码使问题更加清晰。我不知道发生这种情况的根本原因,但我确实有一个解决方法,我会更新我的答案。
  • 谢谢@Mike。我做了类似的事情,甚至没有使用节,将 n 个空单元格添加到静态表的末尾,并以编程方式隐藏它们。虽然它有效,但动态单元格的数量是可变的(我不能授予它少于 10 或 50 个),所以我认为这不是一个可靠的解决方案 :(
  • 我认为你最好的选择是不要在这里使用解决方法,当 Apple 稍后进行更改时,它可能会回来咬你。如果您使用我最初的建议来创建第二个 tableview 控制器,您可能会省去很多麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多