How do I achieve the parent view of the sender?
I have a button inside a custom cell. Clicking the button displays the view through the modal transition.
So here is my question, in prepareForSegue: sender: I want to pull out the indexPath of the cell that contains that (sender) button I clicked. How do I get this cell's indexPath?
I thought I could do something like:
UITableViewCell * cell = sender.parent
Obviously this doesn't work.
Please help me.
UPDATE . Thanks to @rmaddy I tried [[sender superview] superview]
and ended up in the cell where the button was held and to that indexPath. Thanks for your answers guys!
source to share
Assuming it sender
is of UIVIew
some type (for example UIButton
), then:
UIView *parentView = [(UIView *)sender superview];
If you are sure that the superiew button is a table cell, you can do the following:
UITableViewCell *cell = (UITableViewCell *)[(UIView *)sender superview];
Once you have a cell, use the table method indexPathForCell:
.
Keep in mind that if you've actually added a button to a cell, contentView
or some other subset of a cell, then getting the cell from the button is a little trickier.
source to share
The cell is added hidden element: UITableViewCellScrollView
. UITableViewCellScrollView
added between UITableViewCell
and content view. The first sender supervisor returns an object UIView
that matches the content view. The second supervisor will return UITableViewCellScrollView
and the third supervisor will return UITableViewCell
. Then use the method indexPathForCell
to get the cell index.
UITableViewCell *cellView = (UITableViewCell *)[[[sender superview] superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cellView];
source to share