【问题标题】:Circle View is gone when press UITableViewCell按 UITableViewCell 时,圆形视图消失了
【发布时间】:2015-07-23 01:53:44
【问题描述】:
在 UITableViewCell 的 contentView 中,我放了一个UIView 并将其图层cornerRadius 设置为其高度的一半,使其成为一个圆形。并将其颜色设置为红色。
当我运行它并按下UITableViewCell时,红色圆圈变成透明的。
在按下单元格之前。
按下单元格后。
哪里出错了,我觉得和cornerRadius有关。谁能帮帮我?
【问题讨论】:
标签:
ios
uitableview
layer
【解决方案1】:
正如我已经回答here:
UITableViewCell 改变所有子视图的背景颜色时
单元格被选中或突出显示。
我已经根据你的问题调整了我之前的回答
你有三个选择:
-
如果你不想要任何选择样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
子类UITableViewCell 并覆盖Tableview 单元格的setSelected:animated 和/或setHighlighted:animated,如Leo 回答
-
将圆圈添加为图层
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(10, 10, 30, 30);
layer.backgroundColor = [UIColor redColor].CGColor;
layer.cornerRadius = 20/2.0f;
[cell.contentView.layer addSublayer:layer];
【解决方案2】:
因为 UITableviewCell 在选中时会改变视图背景颜色。
你需要继承 UITableviewCell 的子类,并将背景颜色改回来
截图
代码
class CustomCell:UITableViewCell{
override func setHighlighted(highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
let view = self.viewWithTag(10) //The tag of this view is 10
view?.backgroundColor = UIColor.redColor()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
let view = self.viewWithTag(10)
view?.backgroundColor = UIColor.redColor()
}
}