Mich
Mich

Reputation: 112

Changing Button Color in UITableviewCell

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];
}

Upvotes: 1

Views: 1679

Answers (3)

Mich
Mich

Reputation: 112

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.

Upvotes: 0

Mahesh Agrawal
Mahesh Agrawal

Reputation: 3358

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.

Upvotes: 1

tek3
tek3

Reputation: 2115

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];
    }
}

Upvotes: 2

Related Questions