从ios中的Plist嵌套的可扩展UITableView [英] Nested Expandable UITableView from Plist in ios

查看:169
本文介绍了从ios中的Plist嵌套的可扩展UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用plist在ios中创建可扩展表或视图吗?

Hi is it possible to use plist to create a expandable table or view in ios?

这是plist文件,控件类型表示

Here's the plist file and the control type means

0 =切换

1 =文本字段

我可以迭代plist来创建可扩展的表格视图吗?并且输出应该是这个.如果关闭该开关,则该父级的所有子父级都将折叠.我希望有一个人可以帮助我.提前致谢

can I iterate the plist to create a expandable table view? and the output should be this. And if the switch is off all the sub parent of the parent will collapse. I hope someone can help me. Thanks in Advance :D

推荐答案

让我们从基本概念开始.我将专注于扩展/折叠功能,忽略单元格类型(开关,文本字段等).表视图层次结构受节级别和行级别限制.要求是实现任意数量的嵌套级别.但是我们可以使用indentationLevel属性缩进单元格.表格视图将由一个包含扩展(过滤)单元格并提供级别信息的区域组成.因此,目标是将深层plist层次结构转换为平坦的 ,以便在填充表格视图时更易于操作.

Let's start from basic idea. I'll focus on expand/collapse functionality, ignoring cell types (switches, textfields, whatever). Table view hierarchy is limited with section level and row level. The requirement is to implement any number of nested levels. But we could indent cells using indentationLevel property. Table view would consist of one section with expanded (filtered) cells with level information provided. Therefore, goal is to transform deep plist hierarchy to flat one which is easier to manipulate when populating table view.

plist节点的结构.

节点包含以下属性:

  • 名称(单元格标题)
  • 级别(缩进级别,为简单起见,在plist中进行了硬编码)
  • 折叠(节点是否折叠,是否响应用户交互而更改)
  • 子级(层次结构中的子级节点)

根元素 必须是字典.为了保持顶层节点的顺序不变,我们使用数组作为根容器.

Root element must be a dictionary, if you use dictionaryWithContentsOfFile: for plist loading. To keep the order of top level nodes unchanged we use array as a root container.

数据结构

Plist层次结构由NSMutableDictionary表示(需要可变性,因为我们将更改collapsed属性).表格视图平面层次结构由NSMutableArray(敬请期待)表示.

Plist hierarchy is represented by NSMutableDictionary (mutability is required because we will change collapsed property). Table view flat hierarchy is represented by NSMutableArray (stay tuned).

@interface ViewController ()
{
    NSMutableDictionary *_model;
    NSMutableArray *_items;
}
@end

将plist层次结构转换为扁平化

可能的解决方案是遍历所有节点,选择仅扩展,然后将它们添加到平面数组中.可以递归实现:

Possible solution is to iterate over all nodes, choose expanded only and add them to a flat array. It could be achieved recursively:

- (void)reloadExpandedItems
{
    for (NSDictionary *item in _model[@"items"]) {
        [_items addObject:item];
        [self reloadExpandedItemsForItem:item];
    }
}

- (void)reloadExpandedItemsForItem:(NSDictionary*)item
{
    if ([item[@"collapsed"] boolValue]) {
        return;
    }

    for (NSDictionary *child in item[@"children"]) {
        [_items addObject:child];
        [self reloadExpandedItemsForItem:child];
    }

    return;
}

填充表格视图

我们需要设置缩进级别并响应用户交互(折叠/展开)单元格.

We need to set indentation level and respond to user interaction (collapse/expand) cells.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [_items[indexPath.row][@"level"] integerValue];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *item = _items[indexPath.row];
    item[@"collapsed"] = @(![item[@"collapsed"] boolValue]);  // inverse bool value

    [self reloadItems];
}

- (void)reloadItems
{
    [_items removeAllObjects];
    [self reloadExpandedItems];
    [self.tableView reloadData];
}

reloadItems用新的更改重新转换plist层次结构.如果要动画化更改,则需要额外的逻辑来插入/删除扩展/折叠的单元格.

reloadItems retransforms plist hierarchy with new changes. If you want to animate changes you need extra logic for inserting/deleting expanded/collapsed cells.

这篇关于从ios中的Plist嵌套的可扩展UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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