1

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
}
WL APP
  • 13
  • 4

3 Answers3

1

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!

Community
  • 1
  • 1
JBA
  • 2,889
  • 1
  • 21
  • 38
0

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.

Jasmeet Singh
  • 564
  • 3
  • 9
0

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.

Alex W.
  • 202
  • 2
  • 9