RATreeView+TableViewDataSource.m 2.6 KB

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