Skip to content

Instantly share code, notes, and snippets.

@joslinm
Last active December 15, 2015 11:18

Revisions

  1. joslinm revised this gist Apr 8, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions objc_runtime_example.mm
    Original file line number Diff line number Diff line change
    @@ -73,8 +73,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
    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. for example, you might have a tap gesture recognizer on
    // a user's avatar to click to their profile. we're going to just do it on cell for simplicity's sake
    // 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);
  2. joslinm renamed this gist Mar 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. joslinm created this gist Mar 27, 2013.
    104 changes: 104 additions & 0 deletions objc_runtime_example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    //
    // 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. for example, you might have a tap gesture recognizer on
    // a user's avatar to click to their profile. we're going to just do it on cell for simplicity's sake
    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