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!

+3


source to share


5 answers


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.

+7


source


You can access the viewview with a property superview

so that



sender.superview

      

+1


source


Just add the cellForRow method ...

cellButton.tag = indexPath.row;

      

and return it when that button is clicked or you have a link to a button

0


source


try it

UIButton *btn = (UIButton*)sender;
UITableViewCell * cell = (UITableViewCell*)[btn superview];
NSIndexPath *indx = [myTable indexPathForCell:cell];

      

0


source


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];

      

0


source







All Articles