RATreeView+Private.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2013 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RATreeView+Private.h"
  20. #import "RATreeView+Enums.h"
  21. #import "RATreeNode.h"
  22. #import "RATreeNodeCollectionController.h"
  23. @implementation RATreeView (Private)
  24. @dynamic tableView;
  25. @dynamic treeNodeCollectionController;
  26. - (NSArray *)childrenForItem:(id)item
  27. {
  28. NSMutableArray *children = [NSMutableArray array];
  29. NSInteger numberOfChildren = [self.dataSource treeView:self numberOfChildrenOfItem:item];
  30. for (int i = 0; i < numberOfChildren; i++) {
  31. [children addObject:[self.dataSource treeView:self child:i ofItem:item]];
  32. }
  33. return [NSArray arrayWithArray:children];
  34. }
  35. - (void)setupTreeStructure
  36. {
  37. self.treeNodeCollectionController = [[RATreeNodeCollectionController alloc] init];
  38. [self setupTreeStructureWithParentNode:nil treeDepthLevel:0];
  39. }
  40. - (void)setupTreeStructureWithParentNode:(RATreeNode *)parentTreeNode treeDepthLevel:(NSInteger)treeDepthLevel
  41. {
  42. NSArray *children = [self childrenForItem:parentTreeNode.item];
  43. for (id item in children) {
  44. BOOL expanded = NO;
  45. if ([self.dataSource respondsToSelector:@selector(treeView:shouldItemBeExpandedAfterDataReload:treeDepthLevel:)]) {
  46. expanded = [self.delegate treeView:self shouldItemBeExpandedAfterDataReload:item treeDepthLevel:treeDepthLevel];
  47. }
  48. RATreeNode *treeNode = [[RATreeNode alloc] initWithItem:item parent:parentTreeNode expanded:expanded];
  49. [self setupTreeStructureWithParentNode:treeNode treeDepthLevel:(treeDepthLevel + 1)];
  50. [self.treeNodeCollectionController addTreeNode:treeNode];
  51. }
  52. }
  53. - (RATreeNode *)treeNodeForIndex:(NSInteger)index
  54. {
  55. return [self.treeNodeCollectionController treeNodeForIndex:index];
  56. }
  57. - (NSIndexPath *)indexPathForItem:(id)item
  58. {
  59. return [NSIndexPath indexPathForRow:[self.treeNodeCollectionController indexForItem:item] inSection:0];
  60. }
  61. #pragma mark Collapsing and Expanding Rows
  62. - (void)collapseCellForTreeNode:(RATreeNode *)treeNode
  63. {
  64. [self collapseCellForTreeNode:treeNode withRowAnimation:self.rowsCollapsingAnimation];
  65. }
  66. - (void)collapseCellForTreeNode:(RATreeNode *)treeNode withRowAnimation:(RATreeViewRowAnimation)rowAnimation
  67. {
  68. [self.tableView beginUpdates];
  69. NSMutableArray *indexes = [NSMutableArray array];
  70. for (NSInteger index = [treeNode startIndex] + 1; index <= [treeNode endIndex]; index++) {
  71. [indexes addObject:[NSIndexPath indexPathForRow:index inSection:0]];
  72. }
  73. [treeNode collapse];
  74. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:rowAnimation];
  75. [self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:tableViewRowAnimation];
  76. [self.tableView endUpdates];
  77. }
  78. - (void)expandCellForTreeNode:(RATreeNode *)treeNode
  79. {
  80. [self expandCellForTreeNode:treeNode withRowAnimation:self.rowsExpandingAnimation];
  81. }
  82. - (void)expandCellForTreeNode:(RATreeNode *)treeNode withRowAnimation:(RATreeViewRowAnimation)rowAnimation
  83. {
  84. [self.tableView beginUpdates];
  85. [treeNode expand];
  86. NSMutableArray *indexes = [NSMutableArray array];
  87. for (NSInteger index = [treeNode startIndex] + 1; index <= [treeNode endIndex]; index++) {
  88. [indexes addObject:[NSIndexPath indexPathForRow:index inSection:0]];
  89. }
  90. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:rowAnimation];
  91. [self.tableView insertRowsAtIndexPaths:indexes withRowAnimation:tableViewRowAnimation];
  92. [self.tableView endUpdates];
  93. }
  94. @end