Skip to content

Instantly share code, notes, and snippets.

@warren-gavin
Last active June 11, 2016 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warren-gavin/abb0b6e2628e16977b240ba4375e4173 to your computer and use it in GitHub Desktop.
Save warren-gavin/abb0b6e2628e16977b240ba4375e4173 to your computer and use it in GitHub Desktop.
@interface ViewController(Refactor)
@property (nonnull, readonly) NSArray *cellInfo;
@end
@implementation ViewController (Refactor)
- (NSArray *)cellInfo {
return @[ @{ @"text": @"Awful code", @"block": ^{ [self showAlert]; } },
@{ @"text": @"Really awful code", @"block": ^{ [self segue]; } } ];
}
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad"
message:@"Terrible"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop"
style:UIAlertActionStyleCancel
handler:NULL]];
[self presentViewController:alert animated:YES completion:NULL];
}
- (void)segue {
[self performSegueWithIdentifier:@"Unforgivable" sender:self];
}
@end
@implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
void (^operation)() = self.cellInfo[indexPath.row][@"block"];
operation();
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (indexPath.row == 0) {
cell.textLabel.text = @"Awful code";
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"Really awful code";
}
else if (indexPath.row == 2) {
cell.textLabel.text = @"Make it stop";
}
else if (indexPath.row == 3) {
cell.textLabel.text = @"Ohmygodwereallgoingtodie";
}
else {
cell.textLabel.text = @"You don't do this, do you?";
}
return cell;
}
@implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (indexPath.row == 0) {
cell.textLabel.text = @"Awful code";
}
else {
cell.textLabel.text = @"Really awful code";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad"
message:@"Terrible"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop"
style:UIAlertActionStyleCancel
handler:NULL]];
[self presentViewController:alert animated:YES completion:NULL];
}
else {
[self performSegueWithIdentifier:@"Unforgivable" sender:self];
}
}
@end
@implementation NSInvocation (Apokrupto)
+ (instancetype)invocationForSelector:(SEL)selector target:(id)target {
NSMethodSignature *signature = [target methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
invocation.target = target;
return invocation;
}
@end
@interface ViewController(Refactor)
@property (nonnull, readonly) NSArray *cellInfo;
@end
@implementation ViewController (Refactor)
- (NSArray *)cellInfo {
return @[ @{ @"text": @"Awful code",
@"selector": [NSInvocation invocationForSelector:@selector(showAlert) target:self] },
@{ @"text": @"Really awful code",
@"selector": [NSInvocation invocationForSelector:@selector(segue) target:self] } ];
}
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad"
message:@"Terrible"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop"
style:UIAlertActionStyleCancel
handler:NULL]];
[self presentViewController:alert animated:YES completion:NULL];
}
- (void)segue {
[self performSegueWithIdentifier:@"Unforgivable" sender:self];
}
@end
@implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInvocation *invocation = self.cellInfo[indexPath.row][@"selector"];
[invocation invoke];
}
@end
@implementation ViewController (Refactor)
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad"
message:@"Terrible"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop"
style:UIAlertActionStyleCancel
handler:NULL]];
[self presentViewController:alert animated:YES completion:NULL];
}
- (void)segue {
[self performSegueWithIdentifier:@"Unforgivable" sender:self];
}
@end
@implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (indexPath.row == 0) {
cell.textLabel.text = @"Awful code";
}
else {
cell.textLabel.text = @"Really awful code";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[self showAlert];
}
else {
[self segue];
}
}
@end
@interface ViewController(Refactor)
@property (nonnull, readonly) NSArray *cellInfo;
@end
@implementation ViewController (Refactor)
- (NSArray *)cellInfo {
return @[ @{ @"text": @"Awful code", @"selector": @selector(showAlert) },
@{ @"text": @"Really awful code", @"selector": @selector(segue) } ];
}
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad"
message:@"Terrible"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop"
style:UIAlertActionStyleCancel
handler:NULL]];
[self presentViewController:alert animated:YES completion:NULL];
}
- (void)segue {
[self performSegueWithIdentifier:@"Unforgivable" sender:self];
}
@end
@implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSelector:self.cellInfo[indexPath.row][@"selector"]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment