title: Custom UITableViewCell elements not appearing ONLY on iPad
body: <p>I ran into the followin issue when running my application on an iPad. The UI elements of my custom first row UITableViewCell were not appearing. I'm allocing and positioning these elements manually so the issue is not related to using the wrong xib. I've uploaded the result of running the exact same code on an <a href="https://i.stack.imgur.com/BTrsD.png" rel="nofollow noreferrer">iPhone 6</a> vs an <a href="https://i.stack.imgur.com/0iDr4.png" rel="nofollow noreferrer">iPad 2</a>. Notice how the first row cell is empty on the iPad but correctly has the two <code>UITextField</code> elements and the <code>UIButton</code> on the iPhone 6</p>
  
  <p>After some experimentation I know what causes the issue now, but I'm at a loss as to why. Here is my implementation of <code>tableView:cellForRowAtIndexPath:</code></p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *summonerCellIdentifier = @"summonerCellIdentifier";
      static NSString *footerCellIdentifier = @"footerCellIdentifier";
      static NSString *headerCellIdentifier = @"headerCellIdentifier";
  
      UITableViewCell *cell;
      if (indexPath.row == 0) {
          // header view 1
          cell = [tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
          if (cell == nil) {
              cell = [UITableViewCell new];
          }
          cell.backgroundColor = [UIColor LHBeigeLight];
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
          self.summonerName = [[UITextField alloc] initWithFrame:CGRectMake(0,30,200,40)];
          [self.summonerName setCenter:CGPointMake(self.view.center.x, self.summonerName.center.y)];
          [self.summonerName setBackgroundColor:[UIColor whiteColor]];
          [self.summonerName setLeftViewMode:UITextFieldViewModeAlways];
          [self.summonerName setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]];
          [self.summonerName setPlaceholder:@"Summoner Name"];
          [self.summonerName setKeyboardType:UIKeyboardTypeEmailAddress];
          [self.summonerName setAutocapitalizationType:UITextAutocapitalizationTypeNone];
          [self.summonerName setAutocorrectionType:UITextAutocorrectionTypeNo];
          // [self.summonerName setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
          [cell.contentView addSubview:self.summonerName];
  
          self.summonerRegion = [[UITextField alloc] initWithFrame:CGRectMake(0,80,200,40)];
          [self.summonerRegion setCenter:CGPointMake(self.view.center.x, self.summonerRegion.center.y)];
          [self.summonerRegion setBackgroundColor:[UIColor whiteColor]];
          [self.summonerRegion setLeftViewMode:UITextFieldViewModeAlways];
          [self.summonerRegion setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]];
          [self.summonerRegion setPlaceholder:@"Summoner Region"];
          [self.summonerRegion setKeyboardType:UIKeyboardTypeEmailAddress];
          [self.summonerRegion setAutocapitalizationType:UITextAutocapitalizationTypeNone];
          [self.summonerRegion setAutocorrectionType:UITextAutocorrectionTypeNo];
          // [self.summonerRegion setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
          [cell.contentView addSubview:self.summonerRegion];
  
          self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0,130,200,40)];
          [self.searchButton setCenter:CGPointMake(self.view.center.x, self.searchButton.center.y)];
          [self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
          [self.searchButton.titleLabel setTextColor:[UIColor whiteColor]];
          [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldColor]] forState:UIControlStateNormal];
          [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldLightColor]] forState:UIControlStateHighlighted];
          [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldLightColor]] forState:UIControlStateSelected];
          [self.searchButton setContentEdgeInsets:UIEdgeInsetsMake(5, 15, 5, 15)];
          [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
          // [self.searchButton setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
          [cell.contentView addSubview:self.searchButton];
  
          _pickerView = [[UIPickerView alloc] init];
          [self.summonerName setDelegate:self];
          [self.summonerRegion setInputView:_pickerView];
          [self.summonerRegion setDelegate:self];
  
          // Initialize Data
          _pickerData = @[@"NA", @"EUW", @"EUNE", @"BR", @"KR", @"LAN", @"LAS", @"OCE", @"RU", @"TR"];
  
          // Connect data
          _pickerView.dataSource = self;
          _pickerView.delegate = self;
          _pickerView.showsSelectionIndicator = YES;
  
          [self.summonerRegion setText:[_pickerData objectAtIndex:0]];
      } else if (indexPath.row == (recentSearchesArray.count+1)) {
          // footer view 1
          cell = [tableView dequeueReusableCellWithIdentifier:footerCellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:footerCellIdentifier];
          }
          cell.frame = CGRectMake(0, 0, self.recentSearchesTable.frame.size.width, 140);
          cell.backgroundColor = [UIColor LHRedColor];
          cell.textLabel.textColor = [UIColor LHBeigeLight];
          cell.textLabel.text = @"Follow Us On Twitter";
          cell.detailTextLabel.textColor = [UIColor LHBeigeLight];
          cell.detailTextLabel.text = @"for app updates and events!";
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
      } else if (indexPath.row == (recentSearchesArray.count+2)) {
          // footer view 2
          cell = [tableView dequeueReusableCellWithIdentifier:footerCellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:footerCellIdentifier];
          }
          cell.frame = CGRectMake(0, 0, self.recentSearchesTable.frame.size.width, 140);
          cell.textLabel.textColor = [UIColor LHBeigeLight];
          cell.backgroundColor = [UIColor LHGreenColor];
          cell.textLabel.text = @"Battlefy Tournaments";
          cell.detailTextLabel.textColor = [UIColor LHBeigeLight];
          cell.detailTextLabel.text = @"Check out our community tournaments!";
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
      } else {
          // regular cell
          Summoner *summoner = [recentSearchesArray objectAtIndex:indexPath.row-1];
  
          cell = [tableView dequeueReusableCellWithIdentifier:summonerCellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:summonerCellIdentifier];
          }
          cell.textLabel.text = summoner.summonerName;
          cell.detailTextLabel.text = [summoner.summonerRegion uppercaseString];
          cell.backgroundColor = [UIColor whiteColor];
          cell.selectionStyle = UITableViewCellSelectionStyleDefault;
      }
      return cell;
  }
  </code></pre>
  
  <p>I commented out and unformatted the three lines that break my custom views in that first cell. For whatever reason if I set a left-right flexible autoresizing mask the cell elements disappear on the iPad version. I was using these <code>UIViewAutoresizing</code> masks to handle rotation (by keeping the elements horizontally centred in the first cell).</p>
  
  <p>Does anyone have any ideas as to why this is happening? If the way I'm setting up this 0 index <code>UITableViewCell</code> is bad practice or if there is something else wrong with my code please let me know!</p>
  
  <hr/>
  
  <p><strong>Edit:</strong> more info. This is what the <code>tableView:heightForRowAtIndexPath:</code> looks like:</p>
  
  <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      if (indexPath.row == 0) {
          return 200;
      } else if (indexPath.row &gt;= recentSearchesArray.count + 1) {
          return 130;
      } else {
          return 44;
      }
  }
  </code></pre>
  
  <p>And these are the outputs of <code>po [self.view recursiveDescription]</code> for iPhone 6:</p>
  
  <pre><code>(lldb) po [self.view recursiveDescription]
  &lt;UIView: 0x7ff3e156d290; frame = (0 0; 320 568); autoresize = W+H; layer = &lt;CALayer: 0x7ff3e156d360&gt;&gt;
     | &lt;UITableView: 0x7ff3e1858e00; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = &lt;NSArray: 0x7ff3e156ad60&gt;; layer = &lt;CALayer: 0x7ff3e1568610&gt;; contentOffset: {0, 0}; contentSize: {320, 0}&gt;
     |    | &lt;UITableViewWrapperView: 0x7ff3e156bbf0; frame = (0 0; 320 568); gestureRecognizers = &lt;NSArray: 0x7ff3e156c660&gt;; layer = &lt;CALayer: 0x7ff3e156c160&gt;; contentOffset: {0, 0}; contentSize: {320, 568}&gt;
     |    | &lt;UIImageView: 0x7ff3e15698b0; frame = (0 565.5; 320 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x7ff3e15294a0&gt;&gt;
     |    | &lt;UIImageView: 0x7ff3e156a5d0; frame = (317.5 561; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x7ff3e156a5b0&gt;&gt;
  </code></pre>
  
  <p>and for iPad 2:</p>
  
  <pre><code>(lldb) po [self.view recursiveDescription]
  &lt;UIView: 0x7869a230; frame = (0 0; 320 568); autoresize = W+H; layer = &lt;CALayer: 0x786a2b20&gt;&gt;
     | &lt;UITableView: 0x78907600; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = &lt;NSArray: 0x78690b00&gt;; layer = &lt;CALayer: 0x786a62c0&gt;; contentOffset: {0, 0}; contentSize: {320, 0}&gt;
     |    | &lt;UITableViewWrapperView: 0x786a9470; frame = (0 0; 320 568); gestureRecognizers = &lt;NSArray: 0x786a8290&gt;; layer = &lt;CALayer: 0x7866ab40&gt;; contentOffset: {0, 0}; contentSize: {320, 568}&gt;
     |    | &lt;UIImageView: 0x786a9160; frame = (0 565; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x7866a750&gt;&gt;
     |    | &lt;UIImageView: 0x78679fe0; frame = (317 561; 3 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x78671490&gt;&gt;
  </code></pre>
  
  <hr/>
  
  <p><strong>Edit 2:</strong> Added dequeue code for best practice</p>
  
  <hr/>
  
  <p><strong>Edit 3:</strong> Switched to better cell init function <code>[UITableViewCell new];</code> as per Brian's suggestions and add subviews to content view rather than cell itself. Still no luck.</p>
  
  <hr/>
  
  <p><strong>Edit 4 FIXED:</strong> In the end I had to switch to using a custom <code>UITableViewCell</code> subclass and doing all the constraints directly in the .xib to fix this issue. I'm not sure what the underlying cause was but if you still want to reproduce it you can probably do so just by making every cell in your test app to be the one I created for the 0 index.</p>
  
  <hr/>
  
  <p><strong>Edit 5 FIXED X 2:</strong> I found the solution to the original issue with the code above. You can view it here <a href="https://stackoverflow.com/a/28185714/740474">https://stackoverflow.com/a/28185714/740474</a></p>