HINSPTableViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // RMTableViewController.m
  3. // HeapInspectorExample
  4. //
  5. // Created by Christian Menschel on 01.09.14.
  6. // Copyright (c) 2014 tapwork. All rights reserved.
  7. //
  8. #import "HINSPTableViewController.h"
  9. #import "HINSPHeapStackInspector.h"
  10. @interface HINSPTableViewController () <UISearchBarDelegate>
  11. @end
  12. @implementation HINSPTableViewController
  13. {
  14. NSArray *_originalDataSource;
  15. UIActivityIndicatorView *_loadingSpinner;
  16. BOOL _isSearching;
  17. }
  18. - (instancetype)init
  19. {
  20. self = [super initWithStyle:UITableViewStylePlain];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithPointerString:(NSString *)pointer
  27. {
  28. self = [self init];
  29. if (self) {
  30. // Retrieve a real object from the pointer
  31. self.inspectingObject = [HINSPHeapStackInspector objectForPointer:pointer];
  32. }
  33. return self;
  34. }
  35. - (instancetype)initWithObject:(id)object
  36. {
  37. self = [self init];
  38. if (self) {
  39. // Retrieve a real object from the pointer
  40. self.inspectingObject = object;
  41. }
  42. return self;
  43. }
  44. - (instancetype)initWithDataSource:(NSArray *)dataSource
  45. {
  46. self = [self init];
  47. if (self) {
  48. // Retrieve a real object from the pointer
  49. self.dataSource = dataSource;
  50. }
  51. return self;
  52. }
  53. #pragma mark - View Life Cycle
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. [self setupSearchBar];
  58. [self.tableView registerClass:[HINSPTableViewCell class] forCellReuseIdentifier:kTableViewCellIdent];
  59. }
  60. - (void)viewWillAppear:(BOOL)animated
  61. {
  62. [super viewWillAppear:animated];
  63. if (_isSearching) {
  64. [self.tableView.tableHeaderView becomeFirstResponder];
  65. }
  66. }
  67. - (void)setupSearchBar
  68. {
  69. UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
  70. CGRect frame = searchBar.frame;
  71. frame.size.width = self.view.bounds.size.width;
  72. frame.size.height = 44.0f;
  73. searchBar.frame = frame;
  74. searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  75. searchBar.delegate = self;
  76. self.tableView.tableHeaderView = searchBar;
  77. _loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  78. [_loadingSpinner hidesWhenStopped];
  79. _loadingSpinner.frame = CGRectMake(11, 11, 20, 20);
  80. [self.tableView.tableHeaderView addSubview:_loadingSpinner];
  81. }
  82. #pragma mark - Table view data source
  83. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  84. {
  85. return 1;
  86. }
  87. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  88. {
  89. return [self.dataSource count];
  90. }
  91. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. HINSPTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdent forIndexPath:indexPath];
  94. // Configure the cell...
  95. NSString *value = self.dataSource[indexPath.row];
  96. cell.detailTextLabel.text = value;
  97. return cell;
  98. }
  99. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  102. }
  103. #pragma mark - SeachBarDelegate
  104. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
  105. {
  106. if ([searchBar.text length] == 0 && !_isSearching) {
  107. [searchBar setShowsCancelButton:YES animated:YES];
  108. _dataSourceUnfiltered = self.dataSource;
  109. _isSearching = YES;
  110. }
  111. }
  112. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
  113. {
  114. if ([searchBar.text length] == 0) {
  115. [searchBar setShowsCancelButton:NO animated:YES];
  116. self.dataSource = _dataSourceUnfiltered;
  117. [self.tableView reloadData];
  118. _isSearching = NO;
  119. }
  120. }
  121. - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
  122. {
  123. searchBar.text = nil;
  124. [searchBar resignFirstResponder];
  125. }
  126. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  127. {
  128. [searchBar resignFirstResponder];
  129. }
  130. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  131. {
  132. [self.tableView.tableHeaderView bringSubviewToFront:_loadingSpinner];
  133. [_loadingSpinner startAnimating];
  134. NSMutableArray *serps = [self.dataSourceUnfiltered mutableCopy];
  135. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  136. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",
  137. searchText];
  138. [serps filterUsingPredicate:predicate];
  139. dispatch_async(dispatch_get_main_queue(), ^{
  140. self.dataSource = [serps copy];
  141. [self.tableView reloadData];
  142. [_loadingSpinner stopAnimating];
  143. });
  144. });
  145. }
  146. @end