title: iOS 7 - Select/Deselect Row properly on click
body: <p>Sorry but I've been stuck with this tricky problem. I have a tableView and once the user clicks it, a checkmark will appear and it is highlighted. However, when the user clicks it again, only the checkmark is gone and the highlight remains. On the third click, the checkmark is gone but I get the highlight. On the fourth click, the tableViewCell is finally cleared. I suspect this has something to do with my <code>didSelectRowAtIndexPath</code> method. </p>
  
  <p>Here is my <code>cellForRowAtIndexPath</code> method. 
  What I basically do here is that I set the next labels and the checkmark if needed.</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      static NSString *ItemCellIdentifier = @"CellIdentifier";
      SearchResultTableViewCell *tableCell =(SearchResultTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier];
  
      //set text labels here
      [tableCell.price setText:[NSString stringWithString:[priceList objectAtIndex:indexPath.row]]];
      [tableCell.itemName setText:[NSString stringWithString:[itemNameList objectAtIndex:indexPath.row]]];
  
      //set check mark and highlights.
      if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
          tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
      }
  
      else{
          tableCell.accessoryType = UITableViewCellAccessoryNone;
      }
  
      return tableCell;
  
  }
  </code></pre>
  
  <p>Here is my <code>didSelectRowAtIndexPath</code> method. In this method, I toggle the checkMark value by switching it to 0 if it's 1 and vice versa, so when the <code>[self.searchResults reloadData];</code> gets called, it knows which rows needs the checkmark and which ones do not. I also set the selected cell as selected or not because I save the index path for the selected rows so when I reload the table, I know which ones were selected and I can highlight it. </p>
  
  <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
      if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
          [checkList replaceObjectAtIndex:indexPath.row withObject:@"0"];
          [[tableView cellForRowAtIndexPath:indexPath] setSelected:NO];
      }
      else{
          [checkList replaceObjectAtIndex:indexPath.row withObject:@"1"];
          [[tableView cellForRowAtIndexPath:indexPath] setSelected:YES];
      }
  
      NSArray *indexPaths = [self.searchResults indexPathsForSelectedRows];
  
      //searchResults is my tableView
      [self.searchResults reloadData];
  
      for (NSIndexPath *indexPath in indexPaths) {
          [self.searchResults selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
      }
  
  }
  </code></pre>
  
  <p>I have a feeling I'm doing the <code>[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO];</code> wrong. How do I properly access the tableViewCell from the tableView object and how do I properly set it as selected or not?</p>
  
  <p>Thanks in advance </p>