我添加了对延迟加载子节点的支持,并更新了Outline 示例项目以演示同步和异步加载。
首先要注意的是节点childItems == nil
被视为叶子,因此视图控制器不会尝试展开或折叠它们。因此,如果要延迟加载节点,则需要将子项设置为空数组。
为同步加载添加了以下willChangeNode
委托方法:
- (TLIndexPathTreeItem *)controller:(TLTreeTableViewController *)controller willChangeNode:(TLIndexPathTreeItem *)treeItem collapsed:(BOOL)collapsed;
只需返回一个包含子项的新项目,它将替换现有项目。创建新项目的最简单方法是使用以下便捷方法:
TLIndexPathTreeItem *newTreeItem = [treeItem copyWithChildren:newChildren];
对于异步加载,在委托方法中发起 fetchdidChangeNode
并调用
[self setNewVersionOfItem:newTreeItem];
在 fetch 的完成处理程序的主线程上(在哪里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];
}
}];
}
}