0

Is there a way in code to add some hidden information to a UITableViewCell?

Use Case:

  • In an activity feed, (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called and each cell has one activity. Each its own unique "activityId".
  • Each cell has some buttons in them (with unique tags) and, when pressed, the given activity is "liked"
  • Each button has an IBOutlet, which would then call the model to then process the like

The code I have for the IBOutlet is:

- (IBAction)buttonUp:(UIButton *)sender {

UIButton *btn = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *) [[btn superview] superview];

UIView *cellView = [cell.contentView viewWithTag: ..myUniqueButtonTag.. ];

NSLog(@"activityId is: %@", ...);
}
Jacob Kranz
  • 921
  • 3
  • 11
  • 24

2 Answers2

6

Sounds like you need to create a custom UITableViewCell class if you want to do more complex things in your cells then the standard ones allow - https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

ansible
  • 3,569
  • 2
  • 18
  • 29
  • The only thing I really need is the activityId... would I really need a whole new subclass for that? – Jacob Kranz Feb 07 '14 at 20:39
  • Well maybe you could use the tag property if your id is a NSNumber. But subclassing UITableViewCell would be easy if all you really need to do is to add a property. And it might be useful to have a subclass in the future if you want to extend what it will do. – ansible Feb 07 '14 at 20:43
  • which I will probably want to (well, will need to, actually). So, this seems the best course of action. Off the top of your head, know of any large pitfalls I should avoid when setting it up? EDIT: will accept this answer as correct after time limit – Jacob Kranz Feb 07 '14 at 20:46
  • The auto layout can get a bit tricky I've found - look at this answer and the github project for an example of custom cells with variable height. I found it helpful. - http://stackoverflow.com/a/18746930/3096507 – ansible Feb 07 '14 at 20:53
0

You shouldn't do the superview shuffle: [[btn superview] superview] because it will break sooner or later.

I agree with @ansible that you should create a custom cell and use a delegate relationship so the button tells the cell it is tapped and the cell tells the delegate. This is the best and most appropriate solution.

For cheaters though, consider using objc_setAssociatedObject on the button to associate the id info with it so that you can access it in the action method.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I've gone ahead and made a custom cell, but when the button is pressed I rely on `[[[btn superview] superview] superview]` to get the custom cell's class. Regarding your first sentence, is there a better way? – Jacob Kranz Feb 08 '14 at 01:31
  • Make the cell the target of the button method and have it forward the action. – Wain Feb 08 '14 at 10:34