在uitableview中选择时更改自定义单元格图像颜色?

时间:2015-09-08 17:34:28

标签: ios objective-c uitableview uiimage tableviewcell

我有一个带有imageview和label的自定义单元格。当用户选择了tableview中的特定单元格时,我想要更改图像颜色或色调。 我设法改变了标签的颜色,但不知道如何处理图像。有什么想法吗?

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

由于

4 个答案:

答案 0 :(得分:6)

您应该更改MenuTableViewCell自定义类中的单元格数据。那里将有一个控制所选突出显示状态的方法。该方法看起来像这个例子,

- (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
}

}

答案 1 :(得分:3)

如果要在选择单元格时更改单元格图像。您可以使用tintColor绘制当前图像。见下面的代码。

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

希望有所帮助!

答案 2 :(得分:1)

在方法tableviewDidSelectRowAtIndexPath中编写此代码

//编辑正确的语法

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

答案 3 :(得分:1)

正确的语法如下:

在方法tableviewDidSelectRowAtIndexPath

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

Saheb Roy的回答让我走上正轨但是cellAtIndexPath必须被cellForRowAtIndexPath取代。

编辑:在上面的代码中,正在做的是拥有两个不同的图像,并根据是否选择了单元格来更改它们。

结合Saheb Roy,longpham和Devster101给出的答案,最后我在自定义单元类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];
    }
}
相关问题