title: Get reference of cell under heightForRowAtIndexPath: in iOS 8
body: <p>I am currently working on a project where I have embedded a <code>UITableView</code> inside <code>UITableViewCell</code>.</p>
  
  <p>What I need to do is to disable the <code>UITableView</code>'s scroll and make the <code>UITableView</code> to fit size of all rows. But as the <code>UITableView</code> inherits from <code>UIScrollView</code>, the use of <strong>Autolayout</strong> does not force <code>UITableView</code> to make the height of cell depending on its <strong>contentSize</strong> (not frame) when returning <code>UITableViewAutomaticDimension</code>.</p>
  
  <h2>iOS 7 Solution</h2>
  
  <p>This was easy achievable until iOS 7, as I get the reference of the cell under <code>heightForRowAtIndexPath:</code> using the code below:</p>
  
  <pre><code>UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  int height = cell.tableView.contentSize.height;
  
  return height;
  </code></pre>
  
  <p>but in iOS 8 it gives <strong>BAD_ACCESS</strong> as iOS 8 calls the <code>heightForRowAtIndexPath:</code> before the <code>cellForRowAtIndexPath:</code> has been called.</p>
  
  <h2>iOS 8 Approach</h2>
  
  <p>Declare a property to keep reference of the cell:</p>
  
  <pre><code>@property (strong, nonatomic) UITableViewCell *prototypeCell
  </code></pre>
  
  <p>Use a method to save the current cell reference to the property in order to use it:</p>
  
  <pre><code>- (id)prototypeCellatIndexPath:(NSIndexPath *)indexPath {
      NSString *cellID = @"MyCell";
  
      if (!_prototypeCell) {
          _prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:cellID];
      }
  
      return _prototypeCell;
  }
  </code></pre>
  
  <p>Get <code>UITableView</code> of the <code>UITableViewCell</code> from the prototype and from its <strong>contentSize</strong> I get the height and I return it under <code>heighForRowAtIndexPath:</code> from the method below:</p>
  
  <pre><code>-(int)heightForThreadAtIndexPath:(NSIndexPath *)indexPath {
      _prototypeCell = [self prototypeCellatIndexPath:indexPath];
  
      [_prototypeCell.contentView setNeedsLayout];
      [_prototypeCell.contentView layoutIfNeeded];
  
      int footer = [_prototypeCell.tableView numberOfSections]*_prototypeCell.tableView.sectionFooterHeight;
      int header = [_prototypeCell.tableView numberOfSections]*_prototypeCell.tableView.sectionHeaderHeight;
  
      int height = ceilf(_prototypeCell.tableView.contentSize.height) + _prototypeCell.tableView.contentOffset.y + _prototypeCell.tableView.contentInset.bottom + _prototypeCell.tableView.contentInset.top + header + footer;
      NSLog(@"%i, %i", (int)ceilf(_prototypeCell.tableView.contentSize.height), height);
  
      return height;
  }
  
  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return [self heightForThreadAtIndexPath:indexPath];
  }
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>The <strong>contentSize.height</strong> that I get back from the prototypeCell is wrong, and it doesn't match the real contentSize of the <code>UITableView</code>, but when I log the real <strong>contentSize</strong> under <strong>CustomCell Class</strong> it shows the correct contentSize, which differs from the one under prototypeCell. </p>
  
  <p>This makes me wondering that maybe I should try to dequeue the cell at a specific state in order to get the correct contentSize, but logs shows the same values.</p>
  
  <p>I have been researching a lot and trying different ideas, but none worked so far. I don't know if anyone has tried to achieve a similar thing as me, and solved this. It will be really nice if you provide me an idea or something.</p>