|
| 1 | +// |
| 2 | +// ASPTableViewController.m |
| 3 | +// |
| 4 | +// Created by Graham Perks on 4/21/14. |
| 5 | +// Copyright (c) 2014 A Single Pixel, LLC. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +// Your viewDidLoad, in a ASPTableViewController subclass, could look ike this: |
| 9 | +// - (void)viewDidLoad |
| 10 | +// { |
| 11 | +// NSURL *url = [[NSBundle mainBundle] URLForResource:@"shoes.json" withExtension:nil]; |
| 12 | +// self.jsonDefinitionURL = url; |
| 13 | +// |
| 14 | +// [super viewDidLoad]; |
| 15 | +// ... |
| 16 | + |
| 17 | +#import "ASPTableViewController.h" |
| 18 | +#import "ASPTableViewCell.h" |
| 19 | + |
| 20 | + |
| 21 | +@interface ASPTableViewController () |
| 22 | +@property (nonatomic, strong) NSMutableArray *sections; |
| 23 | +@property (nonatomic, strong) NSMutableDictionary *rowClasses; |
| 24 | +@end |
| 25 | + |
| 26 | + |
| 27 | +@implementation ASPTableViewController |
| 28 | + |
| 29 | +//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
| 30 | +//{ |
| 31 | +// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 32 | +// if (self) { |
| 33 | +// // Custom initialization |
| 34 | +// } |
| 35 | +// return self; |
| 36 | +//} |
| 37 | + |
| 38 | + |
| 39 | +- (void)viewDidLoad |
| 40 | +{ |
| 41 | + [super viewDidLoad]; |
| 42 | + self.rowClasses = [NSMutableDictionary dictionary]; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +-(void)setJsonDefinitionURL:(NSURL *)url |
| 47 | +{ |
| 48 | + _jsonDefinitionURL = url; |
| 49 | + |
| 50 | + if (url) { |
| 51 | + |
| 52 | + NSError *error = nil; |
| 53 | + NSInputStream *stream = [[NSInputStream alloc] initWithURL:url]; |
| 54 | + [stream open]; |
| 55 | + self.sections = [NSJSONSerialization JSONObjectWithStream:stream |
| 56 | + options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves |
| 57 | + error:&error]; |
| 58 | + |
| 59 | + if (error) { |
| 60 | + NSLog(@"Failed to read table definition %@", url); |
| 61 | + } |
| 62 | + } |
| 63 | + else { |
| 64 | + self.sections = [NSMutableArray array]; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +-(NSMutableDictionary*)cellInfoFromIndexPath:(NSIndexPath*)indexPath |
| 70 | +{ |
| 71 | + NSMutableDictionary *section = self.sections[indexPath.section]; |
| 72 | + NSMutableArray *cells = section[@"rows"]; |
| 73 | + NSMutableDictionary *cellInfo = cells[indexPath.row]; |
| 74 | + |
| 75 | + return cellInfo; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +#pragma mark - Table view delegate |
| 80 | + |
| 81 | +-(void)performActionForCell:(NSMutableDictionary*)cellInfo |
| 82 | +{ |
| 83 | + // Built in actions; we invoke one of these: |
| 84 | + // 1. Perform a selector |
| 85 | + // 2. Push a storyboard's view controller |
| 86 | + // 3. Perform a segue |
| 87 | + NSString *selector = cellInfo[@"selector"]; |
| 88 | + NSString *segue = cellInfo[@"segue"]; |
| 89 | + NSString *storyboardName = cellInfo[@"storyboard"]; |
| 90 | + NSString *viewControllerId = cellInfo[@"viewControllerId"]; |
| 91 | + |
| 92 | + if (selector) { |
| 93 | + SEL sel = NSSelectorFromString(selector); |
| 94 | + IMP imp = [self methodForSelector:sel]; |
| 95 | + void (*func)(id, SEL, NSMutableDictionary*) = (void *)imp; |
| 96 | + func(self, sel, cellInfo); |
| 97 | + } |
| 98 | + else if (storyboardName) { |
| 99 | + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil]; |
| 100 | + UIViewController *vc; |
| 101 | + if (viewControllerId) { |
| 102 | + vc = [storyboard instantiateViewControllerWithIdentifier:viewControllerId]; |
| 103 | + } |
| 104 | + else { |
| 105 | + vc = [storyboard instantiateInitialViewController]; |
| 106 | + } |
| 107 | + |
| 108 | + [self prepareForStoryboard:storyboardName viewControllerId:viewControllerId fromCell:cellInfo destination:vc]; |
| 109 | + [self.navigationController pushViewController:vc animated:YES]; |
| 110 | + } |
| 111 | + else if (segue) { |
| 112 | + [self performSegueWithIdentifier:segue sender:self]; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +-(void)prepareForStoryboard:(NSString *)storyboardName |
| 117 | + viewControllerId:(NSString*)viewControllerId |
| 118 | + fromCell:(NSDictionary*)cellInfo |
| 119 | + destination:(UIViewController*)viewController |
| 120 | +{ |
| 121 | + // Can be overridden to prepare a destination viewController, cf prepareForSegue. |
| 122 | +} |
| 123 | + |
| 124 | +-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
| 125 | +{ |
| 126 | + NSMutableDictionary *cellInfo = [self cellInfoFromIndexPath:indexPath]; |
| 127 | + |
| 128 | + [self performActionForCell:cellInfo]; |
| 129 | +} |
| 130 | + |
| 131 | +#pragma mark - Table View data source |
| 132 | + |
| 133 | +-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
| 134 | +{ |
| 135 | + return self.sections.count; |
| 136 | +} |
| 137 | + |
| 138 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 139 | +{ |
| 140 | + NSDictionary *sectionDefinition = self.sections[section]; |
| 141 | + NSArray *rows = sectionDefinition[@"rows"]; |
| 142 | + return rows.count; |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 147 | +{ |
| 148 | + NSMutableDictionary *rowDefinition = [self cellInfoFromIndexPath:indexPath]; |
| 149 | + NSString *cellId = rowDefinition[@"type"]; |
| 150 | + |
| 151 | + ASPTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath]; |
| 152 | + if ( ! [cell isKindOfClass:[ASPTableViewCell class]]) { |
| 153 | + NSLog(@"Cell must be a subclass of ASPTableViewCell!"); |
| 154 | + } |
| 155 | + |
| 156 | + [cell configureInViewController:self fromRowDefinition:rowDefinition ]; |
| 157 | + |
| 158 | + return cell; |
| 159 | +} |
| 160 | + |
| 161 | +-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath |
| 162 | +{ |
| 163 | + NSMutableDictionary *rowDefinition = [self cellInfoFromIndexPath:indexPath]; |
| 164 | + |
| 165 | + CGFloat height = 44; |
| 166 | + Class class = self.rowClasses[rowDefinition[@"type"]]; |
| 167 | + |
| 168 | + SEL heightSelector = NSSelectorFromString(@"heightForRowDefinition:inViewController:"); |
| 169 | + |
| 170 | + |
| 171 | + if (class && [class respondsToSelector:heightSelector]) { |
| 172 | + |
| 173 | + NSNumber *heightNumber; |
| 174 | + |
| 175 | + // 2 ways to invoke.. |
| 176 | + // 1: |
| 177 | + IMP imp = [class methodForSelector:heightSelector]; |
| 178 | + NSNumber* (*func)(id, SEL, NSMutableDictionary*, UIViewController*) = (void *)imp; |
| 179 | + heightNumber = func(class, heightSelector, rowDefinition, self); |
| 180 | + |
| 181 | +// // 2: |
| 182 | +// NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature methodSignatureForSelector:heightSelector]]; |
| 183 | +// [invocation invokeWithTarget:class]; |
| 184 | +// [invocation getReturnValue:&heightNumber]; |
| 185 | + |
| 186 | + height = [heightNumber doubleValue]; |
| 187 | + } |
| 188 | + |
| 189 | + return height; |
| 190 | +} |
| 191 | + |
| 192 | +#pragma mark - Cell class registration |
| 193 | + |
| 194 | +// Rows must register their classes with us independently from the table view's |
| 195 | +// registration, as we need to call class methods on them before the table view |
| 196 | +// instatiates them. |
| 197 | +-(void)registerClass:(Class)cellClass forCellType:(NSString *)identifier |
| 198 | +{ |
| 199 | + self.rowClasses[identifier] = cellClass; |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +@end |
0 commit comments