title: UITableView and Cell Reuse
body: <p>I have a <code>UITableView</code> and I've subclassed <code>UITableViewCell</code> (called it <code>CustomCell</code>) so it has several labels and a <code>UIImageView</code>.</p>
  
  <p>Only certain cells will actually display an image. Here's my code for <code>tableView:cellForRowAtIndexPath:</code>:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      Match *aMatch = [[appDelegate.matchByDate objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
      static NSString *CellIdentifier = @"Cell";
  
  
      CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
      }
  
  
      cell.homeLabel.text = aMatch.homeTeam;
      cell.awayLabel.text = aMatch.awayTeam;
      cell.timeLabel.text = aMatch.koTime;
      cell.tournamentLabel.text = aMatch.tournament;
  
  
      NSString *tempString = [appDelegate.teamLogos objectForKey:[aMatch homeTeam]];
      if (tempString!=nil) {
          cell.homeImageView.image = [UIImage imageNamed:tempString];
      }
  
      return cell;
  }
  </code></pre>
  
  <p>So it only sets the <code>homeImageView</code> when it finds a corresponding image in a dictionary I have set up. This seems to work for the first few cells, but if I scroll through the list I find cells have an image when they shouldn't have one.</p>
  
  <p>I understand this is probably because of the cell being re-used, but I'm setting the <code>homeImageView</code> after the cell has been created/reused?!</p>
  
  <p>Here's the init method from my CustomCell Class</p>
  
  <pre><code>    - (id)initWithStyle:(UITableViewCellStyle)style
      reuseIdentifier:(NSString *)reuseIdentifier
  {
      if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
          // Initialization code
          tournamentLabel = [[UILabel alloc] init];
          tournamentLabel.textAlignment = UITextAlignmentCenter;
          tournamentLabel.font = [UIFont systemFontOfSize:12];
          tournamentLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];  
          tournamentLabel.textColor = [UIColor darkGrayColor];
          homeLabel = [[UILabel alloc]init];
          homeLabel.textAlignment = UITextAlignmentLeft;
          homeLabel.font = [UIFont systemFontOfSize:16];
          homeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
          awayLabel = [[UILabel alloc]init];
          awayLabel.textAlignment = UITextAlignmentRight;
          awayLabel.font = [UIFont systemFontOfSize:16];
          awayLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
          timeLabel = [[UILabel alloc]init];
          timeLabel.textAlignment = UITextAlignmentCenter;
          timeLabel.font = [UIFont systemFontOfSize:30];
          timeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
          timeLabel.textColor = [UIColor darkGrayColor];
          homeImageView = [[UIImageView alloc]init];
          awayImageView = [[UIImageView alloc]init];
  
  
          [self.contentView addSubview:homeLabel];
          [self.contentView addSubview:awayLabel];
          [self.contentView addSubview:timeLabel];
          [self.contentView addSubview:tournamentLabel];
          [self.contentView addSubview:homeImageView];
          [self.contentView addSubview:awayImageView];
  
      }
      return self;
  }
  </code></pre>