Last active
December 15, 2015 11:18
Revisions
-
joslinm revised this gist
Apr 8, 2013 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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); -
joslinm renamed this gist
Mar 27, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joslinm created this gist
Mar 27, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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