Skip to content

Instantly share code, notes, and snippets.

@joslinm
Last active December 15, 2015 11:18
Show Gist options
  • Save joslinm/5251638 to your computer and use it in GitHub Desktop.
Save joslinm/5251638 to your computer and use it in GitHub Desktop.
<objc/runtime.h> example
//
// BFViewController.m
// ObjcRuntimeExample
//
// Created by Mark Joslin on 3/26/13.
// Copyright (c) 2013 BitFountain. All rights reserved.
//
#import <objc/runtime.h>
#import "BFTableViewControler.h"
// Example user object
@interface User : NSObject
@property int id;
@end
@implementation User
@end
@interface BFTableViewControler ()
@end
@implementation BFTableViewControler
{
NSArray *_users;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Init our fake users
User *user = [[User alloc] init];
user.id = 1;
User *user1 = [[User alloc] init];
user1.id = 2;
User *user2 = [[User alloc] init];
user2.id = 3;
_users = @[user, user1, user2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _users.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell with a tap gesture recognizer on the cell's text label
User *user = _users[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"User %d", user.id];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0f];
cell.textLabel.backgroundColor = [UIColor colorWithWhite:0.8f alpha:0.5f];
// Now let's associate the cell with the user & cell properties on a gesture recognizer to demonstrate
// how you can chain along attributes this way.
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectCellTextLabel:)];
objc_setAssociatedObject(tapGestureRecognizer, @"user", user, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(tapGestureRecognizer, @"cell", cell, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[cell addGestureRecognizer:tapGestureRecognizer];
return cell;
}
- (void)didSelectCellTextLabel:(UITapGestureRecognizer *)tapGestureRecognizer
{
// get the user & text properties
User *user = objc_getAssociatedObject(tapGestureRecognizer, @"user");
UITableViewCell *cell = objc_getAssociatedObject(tapGestureRecognizer, @"cell");
cell.textLabel.text = [NSString stringWithFormat:@"%d user", user.id];
assert(user.id > 0);
assert(cell != nil);
NSLog(@"hooray, we've passed metadata inside of our callback object");
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment