静态单元格内的动态UITableView [英] Dynamic UITableView inside static cell

查看:96
本文介绍了静态单元格内的动态UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read some threads regarding static & dynamic cells incompatibility, but I'm wondering if there's some workaround for my case.

我有一个静态表(由 UITableViewController处理)。我在其中一个单元格中放置了一个动态表格。代表和datasource是两个表的 UITableViewController ,只要内部动态表的行数小于静态表,它就能很好地工作。当动态表具有比静态表更多的单元格时,我得到索引超出界限异常。

I have a static table (handled by a UITableViewController). I put a dynamic table inside one of its cells. Delegate & datasource is the UITableViewController for both tables, and it works pretty well as long as the number of rows for the inner dynamic table is less than the static one. When the dynamic table has more cells than the static one, I get an index i beyond bounds exception.

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

I'm assuming somehow the total number of cells is statically defined and shared by both tables, but can't really understand what's exactly happening. Anybody faced a similar problem and found a workaround?

编辑

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

Here's my numberOfRowsInSection method. In every method of my delegate/datasource, I check wether the calling table is the dynamic one (_tableOptions) or the static (in which case, I call to parent's method).

- (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];
    }
}


推荐答案

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

This is not a question of compatibility. If you opt to implement programmatic control functions for static UITableViews then you have to be sure that you're not conflicting with how they're defined in the storyboard.

例如如果为具有静态单元格的UITableView实现此函数

For example if you implement this function for a UITableView with static cells

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

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

but you've only added 3 static cells for the given view and controller in the storyboard then Xcode cannot simply create 2 more static cells from nothing. I think that the closest thing to a workaround for you is to add another UITableViewController for the table with dynamic cells. You can instantiate the new controller in the current controller that you're using without presenting its view on the screen. Then you can assign the UITableView that has dynamic cells to be the new UITableViewController's tableView property. Likewise, assign the delegate and datasource properties of the UITableView to be the new controller.

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

After seeing the code I do know of one possible workaround. You can leverage the number of sections to trick the UITableViewController into doing what you want.

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

You can use the code below to add arbitrary number of cells to the dynamic view because you're adding the cells to the dynamic table's second section but at the same time setting the number of cells in the static table's second section to 0. The key is that you MUST add the max number of cells to the second section of the static table in the storyboard. These will be dummy cells that are never displayed.

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

In the following image you can see that I set the second section of my static table to be 10 cells and in the code I am able to return up to 10 cells for the dynamic tableview.

- (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];
    }
}

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

You can remove the section headers by implementing this function to clean it up:

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

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

After removing the headers for the sections you get exactly what you're trying to accomplish. There are 10 cells in the dynamic table inside of the third cell of the static table.

这篇关于静态单元格内的动态UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆