0

I'm trying to implement the block method outlined in the answer to the question in this link, but I don't understand how to set the block. Assuming that...

 cell.onButtonTapped = ^{
        [self buttonSelectedAtIndexPath:indexPath];
    }

is pseudo code, how would I actually implement this assuming I have a UITableViewCell (not a custom cell) and a UIButton within that cell. Is it possible to do this without creating my own UITableViewCell subclass?

Community
  • 1
  • 1
Apollo
  • 8,874
  • 32
  • 104
  • 192

2 Answers2

1

If you're gonna do it this way, you'd really have to make a UITableViewCell subclass which is a target of its button, has a property for the block and calls the block when the button is tapped.

There's actually a simple way to know from which indexPath a subview (eg. the button here) of a cell is from. First you'll need a point on the tableView's coordinates, which is contained by the subview. Let's say you'll choose the center of the subview. To convert it to the tableView's coordinates:

CGPoint subViewCenter = [self.tableView convertPoint:subview.center fromView:subView.superView]; // because a view's center is on its superview's coordinates

to get the indexPath:

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:subViewCenter];
riadhluke
  • 880
  • 6
  • 18
-2

No, as per the answer you are referring to, you need a custom UITableViewCell. Essentially you are calling a method on the cell and passing the current indexPath for that cell.

Whether you use the block method or the delegate method you will need a custom UITableViewCell as you need somewhere to store the indexPath and a method to set it.

Paulw11
  • 108,386
  • 14
  • 159
  • 186