自定义 UITableViewCell 选择样式? [英] Custom UITableViewCell selection style?

查看:26
本文介绍了自定义 UITableViewCell 选择样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我的 UITableViewCell 时,当我点击单元格时,背景部分(我的背景图像没有覆盖的区域)变成蓝色.此外,单元格上的所有 UILabel 在单击时都会变成白色,这正是我想要的.

When I click on my UITableViewCell, the background part (the areas that my background image doesn't cover) turns blue when I click on the cell. Also, all of the UILabels on the cell turn to white when it is clicked which is what I want.

但是,当我单击它时,我不想要蓝色背景,但是如果我执行 selectionstylenone,那么我会丢失单元格中 UILabel 的突出显示颜色.

However what I do not want is the blue background when I click it but if I do selectionstylenone, then I lose the highlighted colours for the UILabels in the cell.

那么有什么办法可以在单击单元格时去掉蓝色背景,但保留 UILabel s 的突出显示颜色?

So is there any way to just get rid of the blue background when the cell is clicked but to keep the highlighted colors of the UILabels?

推荐答案

您可以按如下方式执行此操作.将表格单元格的选择样式设置为 UITableViewCellSelectionStyleNone.这将删除蓝色背景突出显示.然后,为了让文本标签以您想要的方式高亮显示,而不是使用默认的 UITableViewCell 类,创建 UITableViewCell 的子类并覆盖 setHighlighted:animated 的默认实现使用您自己的实现,根据突出显示的状态将标签颜色设置为您想要的颜色.

You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and override the default implementation of setHighlighted:animated with your own implementation that sets the label colors to however you want depending on the highlighted state.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}

这篇关于自定义 UITableViewCell 选择样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆