You are not supposed to add your views as direct subviews of a UITableViewCell
. Instead, you are supposed to add them as subviews of the cell's contentView
. If you lay out your cell in a xib, Interface Builder will automatically take care of this for you. So in general you should not expect the cell to be the direct superview of your text field.
I would handle this in one of two ways:
Give
CustomTextField
a weak property that references its containingCustomCell
. Then you can just ask the text field for its cell. If you're using a xib, make the property anIBOutlet
and hook it up in the xib. If you're creating the cell in code, set the property in code when you create the cell and the text field.Walk up the view hierarchy looking for the
CustomCell
ancestor.CustomCell *cell = textField.superview; while (cell && ![cell isKindOfClass:[CustomCell class]]) { cell = [cell superview]; }