在 uitableview 中选择时更改自定义单元格图像颜色?
Change custom cell image color when selected in uitableview?
我有一个带图像视图的自定义单元格,label.When 用户在我想要更改图像颜色或色调的表格视图中选择了一个特定单元格。
我设法改变了标签的颜色,但不知道如何处理图像。有什么想法吗?
- (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;
}
谢谢
在方法tableviewDidSelectRowAtIndexPath
中写入这段代码
//编辑为正确的语法
{
MenuTableViewCell *cell = (MenuTableViewCell *)[tableview cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:@"your image name when you want to change to selected"];
}
正确的语法如下:
在方法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 给出的答案,我最终在自定义单元格中添加了以下代码 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];
}
}
如果您想在选择单元格时更改单元格图像。您可以使用 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];
}
希望对您有所帮助!
您应该更改 MenuTableViewCell 自定义 class 中的单元格数据。那里会有一个方法来控制选中的、突出显示的状态。该方法类似于此示例,
- (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
}
}
我有一个带图像视图的自定义单元格,label.When 用户在我想要更改图像颜色或色调的表格视图中选择了一个特定单元格。 我设法改变了标签的颜色,但不知道如何处理图像。有什么想法吗?
- (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;
}
谢谢
在方法tableviewDidSelectRowAtIndexPath
中写入这段代码
//编辑为正确的语法
{
MenuTableViewCell *cell = (MenuTableViewCell *)[tableview cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:@"your image name when you want to change to selected"];
}
正确的语法如下:
在方法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 给出的答案,我最终在自定义单元格中添加了以下代码 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];
}
}
如果您想在选择单元格时更改单元格图像。您可以使用 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];
}
希望对您有所帮助!
您应该更改 MenuTableViewCell 自定义 class 中的单元格数据。那里会有一个方法来控制选中的、突出显示的状态。该方法类似于此示例,
- (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
}
}