WL APP
WL APP

Reputation: 13

How to get indexpath of the selected row?

I am developing a messenger app. I have a tableview with custom cell containing an imageview and a couple of labels.

I want to load different controllers on tapping different UI elements. To be specific, I want controllerA to be loaded when imageView is tapped and controllerB when the rest of the row is selected.

I placed a button on top of imageview and made a customcellclass delegate to notify my tableviewcontroller when imageview is tapped with the aid of that button - now how am i supposed to get indexpath for the tapped row imageView ??

//MyCustomClassMethod

-(IBAction)loadProfileButtonTapped:(id)sender{
    [self.loadProfileDelegate takeMeToProfile];
}

//i am implementing this method in my tableview controller

-(void)takeMeToProfile{
//need indexpath here
}

Upvotes: 1

Views: 966

Answers (3)

Jasmeet Singh
Jasmeet Singh

Reputation: 564

You don't need to have indexpath for what you want to achieve. When you add these custom views just provide them with a tag to identify them when button click event is triggered.

Upvotes: 0

Alex W.
Alex W.

Reputation: 202

It's common to pass a reference to self in delegate methods for the custom cells so that you can trace the indexPath from the parent VC. Try switching this:

-(void)takeMeToProfile

with this:

-(void)customCell:(CustomCell*)cell didTapTakeMeToProfile;

When you implement this in your CustomCell file, you'll simply call [self.loadProfileDelegate customCell:self didTapTakeMeToProfile]. Now that you have a reference to the cell, you can call tableView:indexPathForCell: from the parent VC and use the resulting NSIndexPath however you see fit.

Upvotes: 0

JBA
JBA

Reputation: 2909

Assuming your table view is named tableView, and that you get reference of the tapped image in a method where you grab the sender:

CGPoint imagePosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:imagePosition]; 

All the credit to this so answer!

Upvotes: 1

Related Questions