1

I am trying to change the color of a button when pressed that is in a tableviewCell. However my code changes the color of every button in the table and not just the one in the cell I selected,

How would I go about just changing the color of the button I pressed.

Please see my code below,

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];
    [addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Test";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];/
    [addNotesButton addTarget:self action:@selector(addNotes :) forControlEvents:UIControlEventTouchUpInside];
}
kb920
  • 3,039
  • 2
  • 33
  • 44
Mich
  • 112
  • 1
  • 9

3 Answers3

2

The main issue might be in your cellForRowAtIndexPath: method. UITableView cells are re-used as they are displayed on the screen. dequeueReusableCellWithIdentifier: method returns a cell if it has been marked as ready for reuse. You must have seen this method of UITableView being used in cellForRowAtIndexPath: method. (See this link)

So in cellForRowAtIndexPath: you will have to configure each cell as it is being loaded or else it will display old values (since the cells are being reused).

You can either declare a property or a simple variable of type NSIndexPath.Let the variable be called _selectedIndexPath. Then in didSelectRowAtIndexPath: you can assign this property to the indexPath selected.

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSArray *indexPaths = nil;
    if (_selectedIndexPath) {
        indexPaths = @[_selectedIndexPath, indexPath];
    } else {
        indexPaths = @[indexPath];

    }
    _selectedIndexPath = indexPath;
    [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Your Cell Identifier"];
    UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];

    if (indexPath.row == _selectedIndexPath.row) {
        [addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    } else {
        [addNotesButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    }
}
Community
  • 1
  • 1
tek3
  • 2,095
  • 2
  • 20
  • 50
  • Hi thank you for the response. I have a problem where indexPath.row == _selectedIndexPath.row its always equal setting my buttons to blue at all times. – Mich Jun 29 '15 at 15:47
  • It still changes the color of all the buttons instead of the button i press. – Mich Jun 29 '15 at 17:43
  • @Mich: Can you please provide the code that you are using? It hard to tell what might be going wrong without seeing the code. – tek3 Jun 29 '15 at 19:26
  • Hi tek3 I edited the question with my code. See above. – Mich Jun 30 '15 at 22:42
  • Hi tek3 I made the changes but all the buttons turn blue even though I only press one. :/ – Mich Jul 02 '15 at 15:31
  • I had to take a different approach. – Mich Jul 03 '15 at 15:23
1

You don't need to change color manually in did select row at index path. Just set the color for UIControlStateSelected and on the action of button tap set the buttons selected property to YES. From your code i think this should work.

inside cell for row at index path method

 [addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];

and in button action method

-(IBAction)addNotes:(id)sender
{
    UIButton *button = (UIButton*)sender;
    buttons.selected = !button.isSelected;
}

I think this will work.

Mahesh Agrawal
  • 3,348
  • 20
  • 34
  • This produces the same result as my original code.all the buttons change colour when I only want the button i selected to change colour. – Mich Jul 02 '15 at 15:07
0

After trying everything and failed. I ended up having a hidden value in each row that would change when the button is pressed. So the code reads the value then configures the button for each row.

Mich
  • 112
  • 1
  • 9