ios - TLIndexPathTools - 动态加载树 TableView ?

标签 ios uitableview hierarchical-data

我正在使用新的 TLIndexPathTools library创建动态 iOS TableView 。具体来说,使用 TLIndexPathTreeItem 类构建分层下拉表结构。

从“大纲”示例来看,似乎整个层次结构必须从叶节点到父节点静态生成:

//setup item heirarchy for data model
TLIndexPathTreeItem *item111 = [self itemWithId:ITEM_1_1_1 level:2 children:nil];
TLIndexPathTreeItem *item112 = [self itemWithId:ITEM_1_1_2 level:2 children:nil];
TLIndexPathTreeItem *item11 = [self itemWithId:ITEM_1_1  level:1 children:@[item111, item112]];
TLIndexPathTreeItem *item121 = [self itemWithId:ITEM_1_2_1 level:2 children:nil];
TLIndexPathTreeItem *item12 = [self itemWithId:ITEM_1_2 level:1 children:@[item121]];
TLIndexPathTreeItem *item1 = [self itemWithId:ITEM_1 level:0 children:@[item11, item12]];
TLIndexPathTreeItem *item211 = [self itemWithId:ITEM_2_1_1 level:2 children:nil];
TLIndexPathTreeItem *item221 = [self itemWithId:ITEM_2_2_1 level:3 children:nil];
TLIndexPathTreeItem *item212 = [self itemWithId:ITEM_2_1_2 level:2 children:@[item221]];
TLIndexPathTreeItem *item21 = [self itemWithId:ITEM_2_1 level:1 children:@[item211, item212]];
TLIndexPathTreeItem *item2 = [self itemWithId:ITEM_2 level:0 children:@[item21]];

但是,我有大约 1,000 行数据,这些数据在 2-4 个级别之间,直到每个叶子。有没有办法在点击时动态填充每个下拉菜单?否则,我需要一个巨大的递归数据库调用和一些其他的 hackery 来将整个树结构加载到内存中,然后以某种方式从叶到父静态设置它,如示例中所示。

最佳答案

我添加了对延迟加载子节点的支持并更新了 Outline sample project演示同步和异步加载。

首先要注意的是,带有 childItems == nil 的节点被视为叶子,因此 View Controller 不会尝试展开或折叠它们。所以如果你想延迟加载一个节点,你需要将子项设置为一个空数组。

下面的 willChangeNode 委托(delegate)方法已添加用于同步加载:

- (TLIndexPathTreeItem *)controller:(TLTreeTableViewController *)controller willChangeNode:(TLIndexPathTreeItem *)treeItem collapsed:(BOOL)collapsed; 

只需返回一个包含子项的新项,它将替换现有项。创建新项的最简单方法是使用以下便捷方法:

TLIndexPathTreeItem *newTreeItem = [treeItem copyWithChildren:newChildren];

对于异步加载,在didChangeNode委托(delegate)方法中发起fetch并调用

[self setNewVersionOfItem:newTreeItem];

在获取完成处理程序的主线程上(其中 selfTLTreeTableViewController):

编辑 详细说明异步延迟加载,例如如果您从服务器获取数据,它看起来像这样:

- (void)controller:(TLTreeTableViewController *)controller didChangeNode:(TLIndexPathTreeItem *)treeItem collapsed:(BOOL)collapsed
{
    //the logic here checks that the node is being opened. Note that this will cause the
    //node to refresh every time it's expanded. If we only want to load children once,
    //we need to keep track of which nodes have been loaded (or maybe just check
    //that there are no child items yet: [treeItem.childItmes count] == 0).
    if (collapsed == NO) {

        //the next steps is to initiate a data fetch. So the hypothetical method
        //`fetchChildDataFromServerForItem` would execute the appropriate fetch for
        //the given item (to identify the item, one would typically look at the
        //`treeItem.identifier` property or maybe some information that has been placed
        //in `treeItem.data`.
        [self fetchChildDataFromServerForItem:treeItem completion:^(BOOL success, NSArray *result){

            //The fetch method would presumably have a completion block, where the result
            //data would be inserted into the tree.

            if (success && [result count] > 0) {

                //we build up an array of child items by looping over the result set. for arguments
                //sake, assume the result items are dictionaries (e.g. JSON data). They could just
                //as well be Core Data objects, strings, etc.
                NSMutableArray *childItems = [NSMutableArray arrayWithCapacity:result.count];
                for (NSDictionary *data in result) {
                    //TLIndexPathTools relies on items being identifiable, so we need to determine
                    //an appropriate identifier for our data. It could be the data object itself if it
                    //has a suitable `isEqual` method, such as with `NSString`. For a Core Data object,
                    //we would use the `objectID`. But for our dictionary items, we'll assume the
                    //dictionary contains a `recordId`.
                    NSString *identifier = [data valueForKey:@"recordId"];
                    //now we wrap our data in a tree item and add it to the array
                    TLIndexPathTreeItem *childItem = [[TLIndexPathTreeItem alloc] initWithIdentifier:identifier sectionName:nil cellIdentifier:@"CellIdForThisLevel" data:data andChildItems:nil];
                    [childItems addObject:childItem];
                }
                //now we generate a new new node by copying the existing node, but providing a new set of children
                TLIndexPathTreeItem *newTreeItem = [treeItem copyWithChildren:childItems];
                //and finally ask the view controller to set our new tree item, which will cause the new
                //child items to appear in the table.
                [self setNewVersionOfItem:newTreeItem];

            } else {

                //perhaps if the fetch fails to return any items, we set the child items to nil
                //to indicate to the controller that this is a leaf node.
                TLIndexPathTreeItem *newTreeItem = [treeItem copyWithChildren:nil];
                [self setNewVersionOfItem:newTreeItem];

            }
        }];
    }
}

关于ios - TLIndexPathTools - 动态加载树 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17727210/

相关文章:

iphone - 适用于 iOS 的线性规划库

ios - 我如何为 IOS 导出我的 Flex 项目

ios - 在 Tableview 单元格内的 webview 上加载 HTML 内容需要太多时间?

sql - Oracle 中的分层查询 - 失控?

php - 有没有办法从 SQL 查询返回绑定(bind)结果?

ios - 我们是否被迫使用应用内购买来获取国际电话服务?

ios - 来回导航时清理旧的 UITableViewCells

ios - UITableView 像 Swift 中的 UIPickerView 一样选择了行中心

php - php中的树数据结构

ios - 使用 AFNetworking 2 和可达性没有互联网连接警报