title: How to use Reusable Cells in uitableview for IOS
body: <p>Ok. I cant seem to get a firm understanding on how tableviews work. Would someone please explain to me how cells are reused in tableviews especially when scrolling? One of the major pain points I have about this is the fact that when I create an action in one cell, other cells are affected when I scroll. I tried using an array as the backend for the model but still I get cells that change when not suppose to. The hard thing to figure out is why do they change when the the model in the array is not changed. </p>
  
  <p>A simple example: </p>
  
  <p>table view cells with the button "like". When I click the button in one of the cells, the button text changes to "Unlike"(So far so good). But When I scroll down, other cells also show "Unlike" even though I haven't selected them. And when I scroll up, the cells I originally selected change again and newer ones are changed as well. </p>
  
  <p>I cant seem to figure this out. If you can show me a working example source code, that would be awesome!!! Thanks! </p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
  
      likeState = [[NSMutableArray alloc]init];
  
      int i =0;
      for (i=0; i&lt;20; i++) {
          [likeState addObject:[NSNumber numberWithInt:0]];
      }
  } 
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  
      UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [myButton setTitle:@"Like" forState:UIControlStateNormal];
      [myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
      myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
      myButton.tag =indexPath.row;
      [cell.contentView addSubview:myButton];
  
      if (cell ==nil) {
  
  
      }
  
      if ([[likeState objectAtIndex:indexPath.row]boolValue]==NO) {
          [myButton setTitle:@"Like" forState:UIControlStateNormal];
  
      }
      else{
          [myButton setTitle:@"Unlike" forState:UIControlStateNormal];
  
  
      }
  
  
      return cell;
  }
  -(void)tapped:(UIButton *)sender{
  
  [likeState replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithInt:1]];
  
      [sender setTitle:@"Unlike" forState:UIControlStateNormal];
  
  }
  </code></pre>