Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define a delegate on the class associated with the Cell's prototype.</p> <p>// MyCell.h</p> <pre><code>@protocol MyCellDelegate - (void)buttonTappedOnCell:(MyCell *)cell; @end @interface MyCell : UITableViewCell @property (nonatomic, weak) id &lt;MyCellDelegate&gt; delegate; @end </code></pre> <p>// MyCell.m</p> <pre><code>@implementation MyCell - (void)buttonTapped:(id)sender { [self.delegate buttonTappedOnCell:self]; } } @end </code></pre> <p>Now go to the class you want to make the Cell's delegate. This is probably going to be a UITableView subclass. In the cellForRowAtIndexPath method make sure you assign the delegate of the Cell to self. Then implement the method specified in the protocol.</p> <pre><code>- (void)buttonTappedOnCell:(MyCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; int row = indexPath.row; } </code></pre> <p><strong>Or if you would prefer a blocks based approach:</strong></p> <p>// MyCell.h</p> <pre><code>typdef void(^CellButtonTappedBlock)(MyCell *cell); @interface MyCell : UITableViewCell @property (nonatomic, copy) CellButtonTappedBlock buttonTappedBlock; @end </code></pre> <p>Then in your tableView's dataSource:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = .... __weak typeof(self) weakSelf = self; [cell setButtonTappedBlock:^(MyCell *cell) { NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:cell]; // Do stuff with the indexPath }]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload