A Quick Cheat I’ve Used

Given a UITableViewCell, you can get its indexPath and, thus, the model object it represents.

But you can also cheat at it. I’ve done this in the past, and I’m not sure how I feel about it.

#import <objc/runtime.h>

The above line should be scary. Just because you can build planets out of proto-matter doesn’t mean that you should.

static const char *BSAssociatedModelObjectKey = "BSAssociatedModelObject";

In your view controller, in tableView:cellForRowAtIndexPath:, after getting the cell and looking up the model object:

objc_setAssociatedObject(cell, BSAssociatedModelObjectKey, modelObject, OBJC_ASSOCIATION_RETAIN);

Then, later, when you have a cell and needs its model object:

modelObject = objc_getAssociatedObject(cell, BSAssociatedModelObjectKey);

This way the model object is associated with the UITableViewCell — but the UITableViewCell itself doesn’t know that or know how to look at it. It’s just a convenience for the view controller.

I’m not sure that the convenience is worth it — it’s not like it’s hard to go from a cell to its model object. And any time you’re getting the runtime involved is weird. Red flags go up — with your name on them, embroidered with radium thread.

But. I can’t help but like it anyway, even though I don’t actually use associated objects in this way. (Anymore.)

If this is all new to you, see Ole Begemann’s Faking instance variables in Objective-C categories with Associative References.

31 Dec 2012

Archive