简体   繁体   中英

Change custom cell image color when selected in uitableview?

I have a custom cell with imageview and label.When user selected a particular cell in the tableview I want change the image color or tint. I managed to change the color of the label , but dont know how to do with the image. Any ideas?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

         static NSString *CellIdentifier = @"MenuCell";

        MenuTableViewCell *cell = (MenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            cell = [[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
           // cell.selectionStyle = UITableViewCellSelectionStyleNone;

        }


        cell.lblTitle.text = [[_items objectAtIndex:[indexPath row]] valueForKey:@"title"];
        cell.imgIcon.image = [UIImage imageNamed:[[_items objectAtIndex:[indexPath row]] valueForKey:@"image"]];
        cell.lblTitle.highlightedTextColor = [UIColor colorWithRed:0.839 green:0.682 blue:0.047 alpha:1];
       cell.selectionStyle = UITableViewCellSelectionStyleGray;
        return cell;
    }

Thanks

You should be changing the cell data inside your MenuTableViewCell custom class. There will be a methods in there that control the selected, highlighted state. The method will look something like this example,

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
    //Change the text colour & image 
} else {
    //Change it back to whatever is was
}

}

If you want to change cell image when you selected a cell. You can draw current image with a tintColor . See below code.

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

    // Change cell image color
    UIImage * image = [cell.imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    cell.imgIcon.image = image;
    cell.imgIcon.tintColor = [UIColor redColor]; // Your tint color
    [cell.imgIcon tintColorDidChange];
}

Hope that helps!

In the method tableviewDidSelectRowAtIndexPath write this code

//Edited to the correct syntax

{
  MenuTableViewCell *cell = (MenuTableViewCell *)[tableview cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:@"your image name when you want to change to selected"];
}

The correct syntax is the following:

In the method tableviewDidSelectRowAtIndexPath

MenuTableViewCell *cell = (MenuTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image = [UIImage imageNamed:@"your image name when you want to change to selected"];

Saheb Roy's answer put me on the right track but cellAtIndexPath has to be replaced by cellForRowAtIndexPath.

EDITED: in the above code what was being done was to have two different images and change them depending on whether the cell was selected or not.

Combining the answers given by Saheb Roy, longpham and Devster101 finally i added the following code in custom cell class MenuTableViewCell.m:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        UIImage * image = [_imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _imgIcon.image = image;
        _imgIcon.tintColor = [UIColor colorWithRed:0.839 green:0.682 blue:0.047 alpha:1];
        [_imgIcon tintColorDidChange];

    } else {
       UIImage * image = [_imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _imgIcon.image = image;
        _imgIcon.tintColor = [UIColor blackColor];
        [_imgIcon tintColorDidChange];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM