title: iOS - sub views of UITableviewCell do not reload/update
body: <p>I've got a table view where the cells are created as follows:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  
      cell.textLabel.text = @"TempTitle";
  
      cell.textLabel.font = [UIFont fontWithName: @"AppleSDGothicNeo-Bold" size:16.0];
  
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  
      UILabel *numberLabel = [[UILabel alloc] init];
      [numberLabel setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
      [numberLabel setText:[NSString stringWithFormat:@"%@", [self.arrayOne objectAtIndex:indexPath.row]]];
  
      UIButton *buttonDown = [[UIButton alloc] init];
      [buttonDown setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
      buttonDown.tag = indexPath.row;
      [buttonDown addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];
  
      UIButton *buttonUp = [[UIButton alloc] init];
      [buttonUp setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
  
      [cell addSubview:buttonDown];
      [cell addSubview:numberLabel];
      [cell addSubview:buttonUp];
  
      return cell;
  }
  </code></pre>
  
  <p>where <code>self.arrayOne</code> (name changed for this thread) holds integer values that are displayed in each cell.  The method when <code>buttonDown</code> is selected is as following:</p>
  
  <pre><code>- (void) quantityDown:(id)sender {
      int clicked = ((UIButton *)sender).tag;
  
      ... math to lower int by one and save the new value in arrayOne
      ... this part works just fine. The value does go down, as intended.
  
      [self.tableView reloadData];
  }
  </code></pre>
  
  <p>When the tableView reloads, a new label is printed on top of the existing label in that cell, as can be seen in the image below:</p>
  
  <p><img src="https://i.stack.imgur.com/CL0Ko.png" alt="Screen shot"></p>
  
  <p>I can only assume that new buttons are being printed on top of the existing ones as well.  This is both expensive and makes the numbers unreadable (especially if you change it multiple times!).  Leaving the view and coming back to it shows the new numbers cleanly, but only until you start changing them again.</p>
  
  <p>A similar effect happens when I use the UISegmentedControl at the top.  Selecting one or the other changes the contents of the table and runs <code>[self.tableView reloadData]</code>.  The textLabel for each cell reloads just fine when this method is called, but the sub views do not reload, and instead stack upon one another.</p>
  
  <p>How would I go about writing this so that there is only ever one subview in each cell, instead of multiple stacked upon one another?  Something speedy and not expensive on resources.  Maybe removing all subviews and then adding the new ones?  I tried something like </p>
  
  <pre><code>[[cell.contentView viewWithTag:tagVariable]removeFromSuperview];
  </code></pre>
  
  <p>to no avail.  Really, I only need to modify the one cell in the table that the user is clicking in.  Except for when the user uses the UISegmentedControl at the top, then all the cells need to be modified.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Created a custom UITableViewCell class...</p>
  
  <pre><code>@interface SSCustomTableViewCell : UITableViewCell
  
  @property (nonatomic) UILabel *quantity;
  @property (nonatomic) UIButton *down;
  @property (nonatomic) UIButton *up;
  
  @end
  
  @implementation SSWeightsTableViewCell
  
  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
          self.quantity = [UILabel new];
          [self.contentView addSubview:self.quantity];
          self.down = [UIButton new];
          [self.contentView addSubview:self.down];
          self.up = [UIButton new];
          [self.contentView addSubview:self.up];
      }
      return self;
  }
  
  - (void) layoutSubviews {
      [super layoutSubviews];
  
      [self.down setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
      [self.down setBackgroundColor:[UIColor purpleColor]];
  
      [self.up setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
      [self.up setBackgroundColor:[UIColor purpleColor]];
  
      [self.quantity setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
      [self.quantity setTintColor:[UIColor purpleColor]];
  }
  
  @end
  </code></pre>
  
  <p>and then in my UITableView class...</p>
  
  <pre><code>#import "SSCustomTableViewCell.h"
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      [self.tableView
       registerClass:[SSCustomTableViewCell class]
       forCellReuseIdentifier:NSStringFromClass([SSCustomTableViewCell class])
       ];
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  
      UITableViewCell *cell = [tableView 
                               dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
                               forIndexPath:indexPath
                               ];
  }
  </code></pre>
  
  <p>but now all I see is the following...</p>
  
  <p><img src="https://i.stack.imgur.com/pVanT.png" alt="new screen show"></p>
  
  <p>where the subviews are not correctly laid out, nor are they the correct size.  Also, within the <code>cellForRowAtIndexPath</code> method, I cannot access the cell's components.  When I enter</p>
  
  <pre><code>cell.quantity
  </code></pre>
  
  <p>I get an error saying that "Property 'quantity' not found on object of type UITableViewCell*"</p>
  
  <p>So I tried </p>
  
  <pre><code>SSCustomTableViewCell *cell = [tableView 
                               dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
                               forIndexPath:indexPath
                               ];
  </code></pre>
  
  <p>and the error goes away, but it still looks the same when I run it.</p>
  
  <p><strong>Edit 2: Working Solution</strong></p>
  
  <p>All I had to do was add</p>
  
  <pre><code>cell = [[UITableViewCell alloc]init];
  </code></pre>
  
  <p>within the <code>cellForRowAtIndexPath</code> method.  Everything else stayed the same, and no custom classes required.</p>