RATreeView+TableViewDelegate.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <QuartzCore/QuartzCore.h>
  20. #import "RATreeView+TableViewDelegate.h"
  21. #import "RATreeView+Private.h"
  22. #import "RATreeView.h"
  23. #import "RATreeNodeCollectionController.h"
  24. #import "RATreeNode.h"
  25. @implementation RATreeView (TableViewDelegate)
  26. #pragma mark Configuring Rows for the Table View
  27. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  28. {
  29. if ([self.delegate respondsToSelector:@selector(treeView:heightForRowForItem:treeNodeInfo:)]) {
  30. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  31. return [self.delegate treeView:self heightForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  32. }
  33. return self.tableView.rowHeight;
  34. }
  35. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  36. {
  37. if ([self.delegate respondsToSelector:@selector(treeView:estimatedHeightForRowForItem:treeNodeInfo:)]) {
  38. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  39. return [self.delegate treeView:self estimatedHeightForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  40. }
  41. return UITableViewAutomaticDimension;
  42. }
  43. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
  44. {
  45. if ([self.delegate respondsToSelector:@selector(treeView:indentationLevelForRowForItem:treeNodeInfo:)]) {
  46. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  47. return [self.delegate treeView:self indentationLevelForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  48. }
  49. return 0;
  50. }
  51. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  52. {
  53. if ([self.delegate respondsToSelector:@selector(treeView:willDisplayCell:forItem:treeNodeInfo:)]) {
  54. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  55. [self.delegate treeView:self willDisplayCell:cell forItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  56. }
  57. }
  58. #pragma mark Managin Accessory Views
  59. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  60. {
  61. if ([self.delegate respondsToSelector:@selector(treeView:accessoryButtonTappedForRowForItem:treeNodeInfo:)]) {
  62. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  63. [self.delegate treeView:self accessoryButtonTappedForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  64. }
  65. }
  66. #pragma mark Managing Selection
  67. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  68. {
  69. if ([self.delegate respondsToSelector:@selector(treeView:willSelectRowForItem:treeNodeInfo:)]) {
  70. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  71. id item = [self.delegate treeView:self willSelectRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  72. NSIndexPath *delegateIndexPath = [self indexPathForItem:item];
  73. return delegateIndexPath.row == -1 ? indexPath : delegateIndexPath;
  74. } else {
  75. return indexPath;
  76. }
  77. }
  78. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  81. if ([self.delegate respondsToSelector:@selector(treeView:didSelectRowForItem:treeNodeInfo:)]) {
  82. [self.delegate treeView:self didSelectRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  83. }
  84. if ([[treeNode treeNodeInfo].children count] == 0) {
  85. return;
  86. }
  87. if (treeNode.expanded) {
  88. if ([self.delegate respondsToSelector:@selector(treeView:shouldCollapaseRowForItem:treeNodeInfo:)]) {
  89. if ([self.delegate treeView:self shouldCollapaseRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]]) {
  90. [self collapseCellForTreeNode:treeNode informDelegate:YES];
  91. }
  92. } else {
  93. [self collapseCellForTreeNode:treeNode informDelegate:YES];
  94. }
  95. } else {
  96. if ([self.delegate respondsToSelector:@selector(treeView:shouldExpandRowForItem:treeNodeInfo:)]) {
  97. if ([self.delegate treeView:self shouldExpandRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]]) {
  98. [self expandCellForTreeNode:treeNode informDelegate:YES];
  99. }
  100. } else {
  101. [self expandCellForTreeNode:treeNode informDelegate:YES];
  102. }
  103. }
  104. }
  105. - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. if ([self.delegate respondsToSelector:@selector(treeView:willDeselectRowForItem:treeNodeInfo:)]) {
  108. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  109. id item = [self.delegate treeView:self willDeselectRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  110. NSIndexPath *delegateIndexPath = [self indexPathForItem:item];
  111. return delegateIndexPath.row == -1 ? indexPath : delegateIndexPath;
  112. } else {
  113. return indexPath;
  114. }
  115. }
  116. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. if ([self.delegate respondsToSelector:@selector(treeView:didDeselectRowForItem:treeNodeInfo:)]) {
  119. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  120. [self.delegate treeView:self didDeselectRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  121. }
  122. }
  123. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. if ([self.delegate respondsToSelector:@selector(treeView:editingStyleForRowForItem:treeNodeInfo:)]) {
  126. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  127. return [self.delegate treeView:self editingStyleForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  128. }
  129. return UITableViewCellEditingStyleNone;
  130. }
  131. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. if ([self.delegate respondsToSelector:@selector(treeView:titleForDeleteConfirmationButtonForRowForItem:treeNodeInfo:)]) {
  134. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  135. return [self.delegate treeView:self titleForDeleteConfirmationButtonForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  136. }
  137. return @"Delete";
  138. }
  139. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. if ([self.delegate respondsToSelector:@selector(treeView:shouldIndentWhileEditingRowForItem:treeNodeInfo:)]) {
  142. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  143. return [self.delegate treeView:self shouldIndentWhileEditingRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  144. }
  145. return YES;
  146. }
  147. #pragma mark Editing Table Rows
  148. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. if ([self.delegate respondsToSelector:@selector(treeView:willBeginEditingRowForItem:treeNodeInfo:)]) {
  151. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  152. [self.delegate treeView:self willBeginEditingRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  153. }
  154. }
  155. - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. if ([self.delegate respondsToSelector:@selector(treeView:didEndEditingRowForItem:treeNodeInfo:)]) {
  158. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  159. [self.delegate treeView:self didEndEditingRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  160. }
  161. }
  162. #pragma mark Tracking the Removal of Views
  163. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. if ([self.delegate respondsToSelector:@selector(treeView:didEndDisplayingCell:forItem:treeNodeInfo:)]) {
  166. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  167. [self.delegate treeView:self didEndDisplayingCell:(RATreeViewCell *)cell forItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  168. }
  169. }
  170. #pragma mark Copying and Pasting Row Content
  171. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  172. {
  173. if ([self.delegate respondsToSelector:@selector(treeView:shouldShowMenuForRowForItem:treeNodeInfo:)]) {
  174. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  175. return [self.delegate treeView:self shouldShowMenuForRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  176. }
  177. return NO;
  178. }
  179. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  180. {
  181. if ([self.delegate respondsToSelector:@selector(treeView:canPerformAction:forRowForItem:treeNodeInfo:withSender:)]) {
  182. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  183. return [self.delegate treeView:self canPerformAction:action forRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo] withSender:sender];
  184. }
  185. return NO;
  186. }
  187. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  188. {
  189. if ([self.delegate respondsToSelector:@selector(treeView:performAction:forRowForItem:treeNodeInfo:withSender:)]) {
  190. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  191. return [self.delegate treeView:self performAction:action forRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo] withSender:sender];
  192. }
  193. }
  194. #pragma mark Managing Table View Highlighting
  195. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. if ([self.delegate respondsToSelector:@selector(treeView:shouldHighlightRowForItem:treeNodeInfo:)]) {
  198. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  199. return [self.delegate treeView:self shouldHighlightRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  200. }
  201. return YES;
  202. }
  203. - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. if ([self.delegate respondsToSelector:@selector(treeView:didHighlightRowForItem:treeNodeInfo:)]) {
  206. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  207. [self.delegate treeView:self didHighlightRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  208. }
  209. }
  210. - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
  211. {
  212. if ([self.delegate respondsToSelector:@selector(treeView:didUnhighlightRowForItem:treeNodeInfo:)]) {
  213. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  214. [self.delegate treeView:self didUnhighlightRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  215. }
  216. }
  217. #pragma mark Private Helpers
  218. - (void)collapseCellForTreeNode:(RATreeNode *)treeNode informDelegate:(BOOL)informDelegate
  219. {
  220. if (informDelegate) {
  221. if ([self.delegate respondsToSelector:@selector(treeView:willCollapseRowForItem:treeNodeInfo:)]) {
  222. [self.delegate treeView:self willCollapseRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  223. }
  224. }
  225. [CATransaction begin];
  226. [CATransaction setCompletionBlock:^{
  227. if ([self.delegate respondsToSelector:@selector(treeView:didCollapseRowForItem:treeNodeInfo:)] &&
  228. informDelegate) {
  229. [self.delegate treeView:self didCollapseRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  230. }
  231. }];
  232. [self collapseCellForTreeNode:treeNode];
  233. [CATransaction commit];
  234. }
  235. - (void)expandCellForTreeNode:(RATreeNode *)treeNode informDelegate:(BOOL)informDelegate
  236. {
  237. if (informDelegate) {
  238. if ([self.delegate respondsToSelector:@selector(treeView:willExpandRowForItem:treeNodeInfo:)]) {
  239. [self.delegate treeView:self willExpandRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  240. }
  241. }
  242. [CATransaction begin];
  243. [CATransaction setCompletionBlock:^{
  244. if ([self.delegate respondsToSelector:@selector(treeView:didExpandRowForItem:treeNodeInfo:)] &&
  245. informDelegate) {
  246. [self.delegate treeView:self didExpandRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  247. }
  248. }];
  249. [self expandCellForTreeNode:treeNode];
  250. [CATransaction commit];
  251. }
  252. @end