I am totally new to iOS development and have been requested by my company to jump into some code written by another amateur that left us and fix it. The main view of this app is just a table view and we would like the user to be able to select one cell from the table, which would highlight it and set some other buttons on the page to do something to that particular cell.

Making the other buttons on the page works just fine, as long as I know what cell is selected, so I am not too worried about that. But the code this other guy wrote to select one cell at a time is atrocious, and the code I tried to replace his with was better, but still very buggy.

My code is this written below (sort of adopted from the old stuff that I don't completely understand). The issue with it is that the background turns black when the cell is clicked twice in a row or if you change views and come back to this table view.

```
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
        [tableView deselectRowAtIndexPath:currentSelection animated:true];
    }

    if(currentSelection.item == indexPath.item){
        [tableView deselectRowAtIndexPath:currentSelection animated:true];

    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //highlight bg to light grey
    cell.backgroundColor =[UIColor colorWithRed:200/255 green:200/255 blue:200/255 alpha:1.0f];
    currentSelection = indexPath;

    //code for other button functions
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //set bg back to white
    cell.backgroundColor =[UIColor colorWithRed:255/255 green:255/255 blue:255/255 alpha:1.0f];
    noSelectedRows = true;
}

```

If that is too terrible too look at and try to fix, I was looking at a tutorial provided by Apple that I wanted to understand a little better anyway. The following code was found here: <https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageSelections/ManageSelections.html>

```
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger catIndex = [taskCategories indexOfObject:self.currentCategory];
    if (catIndex == indexPath.row) {
        return;
    }
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:catIndex inSection:0];

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.currentCategory = [taskCategories objectAtIndex:indexPath.row];
    }

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
}

```

The only things I need to know about that last code is where variables like self.currentCategory and taskCategories are coming from. As of now, there don't seem to be any similar variables in the code I am working with, so I would like to know what I have to point to or create.

This is also my first post on StackOverflow, so if I left out something important, please let me know.