1

The thing about the MVC paradigm is that your view should not know about your model. I have a custom TableCellView, which means it has no direct view controller. It belongs to a TableView. My tableView of course has a controller, MyPetTableViewController.

Anyway, my CustomTableViewCell has a number of buttons and ImageViews, everyone of them clickable. Depending on which button/imageView is clicked, I need to: go to a different tab view, or show a modal. If I were simply listening to table row, then the controller’s job would be straightforward. But how do I get the controller to listen to events within a table cell? I already thought about using touchesEnded:withEvent inside MyPetTableViewController but then I would not know the index in question. so really I need to know: which view inside which table row was clicked?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • 1
    Add target-actions to those buttons in your cell, with the target being the cell itself. Follow a delegation pattern with your view controller as the delegate of the cell. The cell can then tell its delegate when controls are interacted with. – duci9y Jul 26 '14 at 16:27
  • If you are successful, come back and post an answer here for future visitors. If not, tell us and we'd help you further. – duci9y Jul 26 '14 at 16:32
  • @duci9y will do. I already wrote my method in the controller using `tableView:didSelectRowAtIndex:` and `cellForRowAtIndexPath`. Now I am trying to learn how to add target-action from https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/Target-Action/Target-Action.html – Katedral Pillon Jul 26 '14 at 16:55
  • @duci9y here is what I tried doing: from the storyboard I dragged and created IBOutlets for my imageViews. Then inside `awakeFromNib` I tried doing `[self.imageOne addTarget:self]` but I get the error that "No visible interface for UIImageView declares the selector addTarget:". Do you have some ideas how I might add target-actions to my images? – Katedral Pillon Jul 26 '14 at 17:22
  • You can't add a target-action to an image view, it is not meant to be interacted with. Use a button. – duci9y Jul 26 '14 at 17:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58067/discussion-between-katedral-pillon-and-duci9y). – Katedral Pillon Jul 26 '14 at 17:47
  • I am still not seeing how to have the TableView's controller be the one listening to an IBAction that is implemented inside of the TableViewCell. – Katedral Pillon Jul 26 '14 at 18:29
  • take a look at http://stackoverflow.com/questions/4103643/custom-uitableviewcell-and-ibaction. It may help. I see the chat. It is not clear how that would work: that is if the IBAction is declared inside of TableViewCell. @duci9y do you mind showing an example? – Konsol Labapen Jul 26 '14 at 18:31
  • You're welcome. Please post what you did as an answer and accept it, so that future visitors with the same problem can find a solution. – duci9y Jul 26 '14 at 20:47

1 Answers1

0

Here is how I finally implemented the solution

  • I created IBOutlets by dragging from storyboard to the .h file of my CustomTableViewCell. (It's normally my practice to keep IBOutlets in the .m file, but since the solution to this problem needed them public, I had to put them in the .h file).

  • In the Controller where my TableView data source and delegate methods are implemented, inside the method tableView:cellForRowAtIndexPath: I set the target of my buttons. E.g. [cell.imageButtonOne addTarget:self action:@selector(userClicksButtonOne:) forControlEvents:UIControlEventTouchUpInside]

  • then as the final step I implemented userClicksButtonOne as

code:

- (void) userClicksButtonOne:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199