PDFOutlineViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // PDFOutlineViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFOutlineViewController.h"
  11. #define kPDFOutlineItemMaxDeep 10
  12. @interface PDFOutlineModel : NSObject
  13. @property (nonatomic,assign) NSInteger level;
  14. @property (nonatomic,assign) NSInteger hide;
  15. @property (nonatomic,assign) NSInteger count;
  16. @property (nonatomic,assign) NSInteger number;
  17. @property (nonatomic,retain) NSString *title;
  18. @property (nonatomic,assign) BOOL isShow;
  19. @end
  20. @implementation PDFOutlineModel
  21. - (void)dealloc {
  22. [_title release];
  23. [super dealloc];
  24. }
  25. @end
  26. @interface PDFOutlineViewCell : UITableViewCell
  27. @property (nonatomic,assign) IBOutlet UILabel *nameLabel;
  28. @property (nonatomic,assign) IBOutlet UILabel *countLabel;
  29. @property (nonatomic,assign) IBOutlet UIButton *arrowButton;
  30. @property (nonatomic,assign) IBOutlet NSLayoutConstraint *offsetX;
  31. @property (nonatomic,retain) PDFOutlineModel *outline;
  32. @property (nonatomic,assign) BOOL isShow;
  33. @property (nonatomic,assign) id delegate;
  34. @end
  35. @implementation PDFOutlineViewCell
  36. - (void)dealloc {
  37. [_outline release];
  38. [super dealloc];
  39. }
  40. - (void)setOutline:(PDFOutlineModel *)outline {
  41. if (_outline != outline) {
  42. [_outline release];
  43. _outline = [outline retain];
  44. }
  45. self.nameLabel.text = outline.title;
  46. self.countLabel.text = [NSString stringWithFormat:@"%ld",outline.number+1];
  47. self.offsetX.constant = 10*outline.level;
  48. if (outline.count>0) {
  49. [self.arrowButton setHidden:NO];
  50. if (outline.isShow) {
  51. self.arrowButton.selected = YES;
  52. } else {
  53. self.arrowButton.selected = NO;
  54. }
  55. } else{
  56. [self.arrowButton setHidden:YES];
  57. }
  58. }
  59. - (void)setIsShow:(BOOL)isShow {
  60. _isShow = isShow;
  61. self.outline.isShow = self.isShow;
  62. if (self.outline.count>0) {
  63. if (self.outline.isShow) {
  64. self.arrowButton.selected = YES;
  65. } else {
  66. self.arrowButton.selected = NO;
  67. }
  68. }
  69. }
  70. - (IBAction)buttonItemClicked_Arrow:(id)sender {
  71. if ([self.delegate respondsToSelector:@selector(buttonItemClicked_Arrow:)]) {
  72. [self.delegate performSelector:@selector(buttonItemClicked_Arrow:) withObject:self];
  73. }
  74. }
  75. @end
  76. @interface PDFOutlineViewController () <UITableViewDataSource,UITableViewDelegate>
  77. @property (nonatomic,retain) UITableView *tableView;
  78. @property (nonatomic,retain) NSArray *outlines;
  79. @property (nonatomic,retain) UILabel *noDataLabel;
  80. @end
  81. @implementation PDFOutlineViewController
  82. #pragma mark - Init Methods
  83. - (void)dealloc {
  84. [_noDataLabel release];
  85. [_outlines release];
  86. _tableView.delegate = nil;
  87. _tableView.dataSource = nil;
  88. [_tableView release];
  89. [super dealloc];
  90. }
  91. #pragma mark - UIViewController Methods
  92. - (void)loadOutline:(CPDFOutline *)outline level:(NSInteger)level forOutlines:(NSMutableArray *)outlines {
  93. for (int i=0; i<[outline numberOfChildren]; i++) {
  94. CPDFOutline *data = [outline childAtIndex:i];
  95. CPDFDestination *destination = [data destination];
  96. if (!destination) {
  97. CPDFAction *action = [data action];
  98. if (action && [action isKindOfClass:[CPDFGoToAction class]]) {
  99. destination = [(CPDFGoToAction *)action destination];
  100. }
  101. }
  102. PDFOutlineModel *model = [[PDFOutlineModel alloc] init];
  103. model.level = level;
  104. model.hide = (1 << (kPDFOutlineItemMaxDeep - level)) - 1;
  105. model.title = data.label;
  106. model.count = data.numberOfChildren;
  107. model.number = destination.pageIndex;
  108. model.isShow = NO;
  109. [outlines addObject:model];
  110. [self loadOutline:data level:level+1 forOutlines:outlines];
  111. }
  112. }
  113. - (void)viewDidLoad {
  114. [super viewDidLoad];
  115. // Do any additional setup after loading the view.
  116. CPDFOutline *outlineRoot = [self.pdfViewController.pdfView.document outlineRoot];
  117. NSMutableArray *array = [NSMutableArray array];
  118. [self loadOutline:outlineRoot level:0 forOutlines:array];
  119. self.outlines = array;
  120. self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
  121. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  122. self.tableView.dataSource = self;
  123. self.tableView.delegate = self;
  124. self.tableView.rowHeight = UITableViewAutomaticDimension;
  125. self.tableView.estimatedRowHeight = 60;
  126. self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
  127. [self.tableView registerNib:[UINib nibWithNibName:@"PDFOutlineViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
  128. [self.view addSubview:self.tableView];
  129. self.noDataLabel = [[[UILabel alloc] init] autorelease];
  130. self.noDataLabel.textColor = [UIColor grayColor];
  131. self.noDataLabel.text = NSLocalizedString(@"No outlines", nil);
  132. [self.noDataLabel sizeToFit];
  133. self.noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
  134. self.noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  135. [self.view addSubview:self.noDataLabel];
  136. }
  137. #pragma mark - UITableViewDataSource
  138. const static int kShowFlag = (1 << kPDFOutlineItemMaxDeep) -1;
  139. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  140. int n = 0;
  141. for (PDFOutlineModel *outline in self.outlines) {
  142. if (outline.hide == kShowFlag)
  143. n ++ ;
  144. }
  145. if (n > 0) {
  146. self.noDataLabel.hidden = YES;
  147. } else {
  148. self.noDataLabel.hidden = NO;
  149. }
  150. return n;
  151. }
  152. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. int t = -1;
  154. PDFOutlineModel *outline = nil;
  155. for (PDFOutlineModel *model in self.outlines) {
  156. if (model.hide == kShowFlag)
  157. t++;
  158. if (t == indexPath.row) {
  159. outline = model;
  160. break;
  161. }
  162. }
  163. PDFOutlineViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  164. cell.outline = outline;
  165. cell.delegate = self;
  166. return cell;
  167. }
  168. #pragma mark - UITableViewDelegate
  169. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  170. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  171. [self dismissViewControllerAnimated:YES completion:^{
  172. PDFOutlineViewCell *cell = (PDFOutlineViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  173. PDFOutlineModel *outline = cell.outline;
  174. [self.pdfViewController.pdfView goToPageIndex:outline.number animated:NO];
  175. }];
  176. }
  177. #pragma mark - Button Event Action
  178. - (void)buttonItemClicked_Arrow:(PDFOutlineViewCell *)cell {
  179. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  180. int t = -1, p = 0;
  181. PDFOutlineModel *outline = nil;
  182. for (PDFOutlineModel *model in self.outlines) {
  183. if (model.hide == kShowFlag) t++;
  184. if (t == indexPath.row) {
  185. outline = model;
  186. break;
  187. }
  188. p++;
  189. }
  190. if (outline.level == kPDFOutlineItemMaxDeep-1) return;
  191. p++;
  192. if (p == self.outlines.count) return;
  193. PDFOutlineModel *nextOutline = self.outlines[p];
  194. if (nextOutline.level > outline.level) {
  195. if (nextOutline.hide == kShowFlag) {
  196. [(PDFOutlineViewCell *)[self.tableView cellForRowAtIndexPath:indexPath] setIsShow:NO];
  197. NSMutableArray *array = [NSMutableArray array];
  198. while (true) {
  199. if (nextOutline.hide == kShowFlag) {
  200. t ++;
  201. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  202. [array addObject:path];
  203. }
  204. nextOutline.hide ^= 1 << (kPDFOutlineItemMaxDeep - outline.level - 1);
  205. p++;
  206. if (p == self.outlines.count) break;
  207. nextOutline = self.outlines[p];
  208. if (nextOutline.level <= outline.level) break;
  209. }
  210. [self.tableView deleteRowsAtIndexPaths:array
  211. withRowAnimation:UITableViewRowAnimationFade];
  212. } else {
  213. [(PDFOutlineViewCell *)[self.tableView cellForRowAtIndexPath:indexPath] setIsShow:YES];
  214. NSMutableArray *array = [NSMutableArray array];
  215. while (true) {
  216. nextOutline.hide ^= 1 << (kPDFOutlineItemMaxDeep - outline.level - 1);
  217. if (nextOutline.hide == kShowFlag) {
  218. t ++;
  219. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  220. [array addObject:path];
  221. }
  222. p++;
  223. if (p == self.outlines.count) break;
  224. nextOutline = self.outlines[p];
  225. if (nextOutline.level <= outline.level) break;
  226. }
  227. [self.tableView insertRowsAtIndexPaths:array
  228. withRowAnimation:UITableViewRowAnimationFade];
  229. }
  230. }
  231. }
  232. @end