RATreeView+TableViewDataSource.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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+TableViewDataSource.h"
  20. #import "RATreeView+Private.h"
  21. #import "RATreeNodeCollectionController.h"
  22. #import "RATreeNode.h"
  23. @implementation RATreeView (TableViewDataSource)
  24. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  25. {
  26. return 1;
  27. }
  28. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  29. {
  30. if (self.treeNodeCollectionController == nil) {
  31. [self setupTreeStructure];
  32. }
  33. return [self.treeNodeCollectionController.root numberOfVisibleDescendants];
  34. }
  35. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  36. {
  37. RATreeNode *treeNode = [self.treeNodeCollectionController treeNodeForIndex:indexPath.row];
  38. return [self.dataSource treeView:self cellForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  39. }
  40. #pragma mark Inserting or Deleting Table Rows
  41. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  42. {
  43. if ([self.dataSource respondsToSelector:@selector(treeView:commitEditingStyle:forRowForItem:treeNodeInfo:)]) {
  44. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  45. [self.dataSource treeView:self commitEditingStyle:editingStyle forRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  46. }
  47. }
  48. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  49. {
  50. if ([self.dataSource respondsToSelector:@selector(treeView:canEditRowForItem:treeNodeInfo:)]) {
  51. RATreeNode *treeNode = [self treeNodeForIndex:indexPath.row];
  52. return [self.dataSource treeView:self canEditRowForItem:treeNode.item treeNodeInfo:[treeNode treeNodeInfo]];
  53. }
  54. return YES;
  55. }
  56. @end