I’ve subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I’ve enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
- In the subclassed
UITableViewCell, insidelayoutSubviews, create the
CGRects for the “alternate” labels, position them in the same
places as the two “original” label and hide them viasetAlpha:; - When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can’t figure out what logic in layoutSubviews I’d use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can’t figure out how to use that fact to accomplish what I’m trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
I’ll do the following. Create a public method in your
UITableViewCellsubclass like the following:In this method you could set the
contentViewas you prefer.Then in your view controller implement
This is a simple example for change the content. In my opinion you don’t have to use
layoutSubviewsto add views.Leave this logic in
changeContentForCelland then callsetNeedsLayoutto change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.Hope it helps.