|
| 1 | +// |
| 2 | +// ViewController.m |
| 3 | +// TableViewsExample |
| 4 | +// |
| 5 | +// Created by Tom Adriaenssen on 16/05/13. |
| 6 | +// Copyright (c) 2013 Tom Adriaenssen. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ViewController.h" |
| 10 | +#import <objc/runtime.h> |
| 11 | +#import "IIViewDeckController.h" |
| 12 | + |
| 13 | +@interface ViewController () <UISearchBarDelegate, UIGestureRecognizerDelegate> |
| 14 | + |
| 15 | +@property (nonatomic, retain) IBOutlet UISearchBar* searchBar; |
| 16 | + |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation ViewController { |
| 20 | + NSArray* _methods; |
| 21 | + NSArray* _allMethods; |
| 22 | +} |
| 23 | + |
| 24 | +- (id)initWithStyle:(UITableViewStyle)style |
| 25 | +{ |
| 26 | + self = [super initWithStyle:style]; |
| 27 | + if (self) { |
| 28 | + // Custom initialization |
| 29 | + _searchable = YES; |
| 30 | + } |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +- (void)viewDidLoad |
| 35 | +{ |
| 36 | + [super viewDidLoad]; |
| 37 | + |
| 38 | + uint count; |
| 39 | + Method* methodlist = class_copyMethodList([UITableView class], &count); |
| 40 | + |
| 41 | + NSMutableArray* methods = [NSMutableArray new]; |
| 42 | + for (int i=0; i<count; ++i) { |
| 43 | + NSString* name = NSStringFromSelector(method_getName(methodlist[i])); |
| 44 | + if ([name rangeOfString:@"_"].location != 0) |
| 45 | + [methods addObject:name]; |
| 46 | + } |
| 47 | + |
| 48 | + _allMethods = [methods sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]]]; |
| 49 | + [self filter:nil]; |
| 50 | + |
| 51 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(doEdit)]; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)setTintColor:(UIColor *)tintColor { |
| 55 | + _tintColor = tintColor; |
| 56 | + |
| 57 | + self.view; // fdj |
| 58 | + self.searchBar.tintColor = tintColor; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)setSearchable:(BOOL)searchable { |
| 62 | + _searchable = searchable; |
| 63 | + |
| 64 | + self.view; |
| 65 | + if (searchable) { |
| 66 | + self.tableView.tableHeaderView = self.searchBar; |
| 67 | + } |
| 68 | + else { |
| 69 | + self.tableView.tableHeaderView = nil; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +- (void)viewWillAppear:(BOOL)animated { |
| 74 | + [super viewWillAppear:animated]; |
| 75 | + ((UITableViewController*)self.viewDeckController.leftController).tableView.scrollsToTop = NO; |
| 76 | + UINavigationController* nc = self.viewDeckController.centerController; |
| 77 | + ((UITableViewController*)nc.topViewController).tableView.scrollsToTop = NO; |
| 78 | + ((UITableViewController*)self.viewDeckController.rightController).tableView.scrollsToTop = NO; |
| 79 | + self.tableView.scrollsToTop = YES; |
| 80 | +} |
| 81 | + |
| 82 | +- (void)filter:(NSString*)filter { |
| 83 | + if (filter) { |
| 84 | + _methods = [_allMethods filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self beginswith[cd] %@", filter]]; |
| 85 | + } |
| 86 | + else { |
| 87 | + _methods = _allMethods; |
| 88 | + } |
| 89 | + |
| 90 | + [[self tableView] reloadData]; |
| 91 | +} |
| 92 | + |
| 93 | +#pragma mark - search |
| 94 | + |
| 95 | +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { |
| 96 | + [self filter:searchText]; |
| 97 | +} |
| 98 | + |
| 99 | +#pragma mark - Table view data source |
| 100 | + |
| 101 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 102 | +{ |
| 103 | + return _methods.count; |
| 104 | +} |
| 105 | + |
| 106 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 107 | +{ |
| 108 | + static NSString *CellIdentifier = @"Cell"; |
| 109 | + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 110 | + if (cell == nil) { |
| 111 | + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; |
| 112 | + } |
| 113 | + |
| 114 | + cell.textLabel.text = _methods[indexPath.row]; |
| 115 | + cell.textLabel.adjustsFontSizeToFitWidth = YES; |
| 116 | + cell.showsReorderControl = YES; |
| 117 | + |
| 118 | + return cell; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +// Override to support rearranging the table view. |
| 123 | +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath |
| 124 | +{ |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +// Override to support conditional rearranging of the table view. |
| 129 | +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath |
| 130 | +{ |
| 131 | + // Return NO if you do not want the item to be re-orderable. |
| 132 | + return YES; |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +#pragma mark - Table view delegate |
| 137 | + |
| 138 | + |
| 139 | +- (void)doEdit { |
| 140 | + [self.tableView setEditing:!self.tableView.editing animated:YES]; |
| 141 | + if (self.tableView.editing) |
| 142 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doEdit)]; |
| 143 | + else |
| 144 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(doEdit)]; |
| 145 | +} |
| 146 | + |
| 147 | +@end |
0 commit comments