我正在使用新的 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];
在获取完成处理程序的主线程上(其中 self
是 TLTreeTableViewController
):
编辑 详细说明异步延迟加载,例如如果您从服务器获取数据,它看起来像这样:
- (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/