静态单元格内的动态UITableView

ssa*_*tos 3 objective-c uitableview ios

我已经阅读了一些关于静态和动态细胞不兼容的线索,但我想知道我的案例是否有一些解决方法.

我有一个静态表(由a处理UITableViewController).我在其中一个单元格中放置了一个动态表格.Delegate和datasource是UITableViewController两个表的表,只要内部动态表的行数小于静态表的行数,它就能很好地工作.当动态表具有比静态表更多的单元时,我得到一个index i beyond bounds例外.

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

编辑

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

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*e S 6

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

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return 5;
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:看到代码后,我知道一个可能的解决方法.您可以利用部分数量来欺骗UITableViewController,使其成为您想要的.

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

在下图中,您可以看到我将静态表的第二部分设置为10个单元格,在代码中我可以为dynamictableview 返回最多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];
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过实现此功能来清除部分标题以进行清理:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

删除部分的标题后,您将获得正在尝试完成的内容.表中有10 dynamic个单元格位于静态表的第三个单元格内.

在此输入图像描述