[英]Subview in cell loses color when tableview is changing
I have an UITableView which has a dynamic subview. 我有一个具有动态子视图的UITableView。
When the table is static it looks like this: 当表格是静态的时,它看起来像这样: The round view with the T is the custom subview 带T的圆形视图是自定义子视图
But when I choose edit and drag the table cell the it looses it's color and the T. 但是,当我选择编辑并拖动表格单元格时,它失去了颜色和T。
Whats the reason for this? 是什么原因呢?
I initialize the cell like this (It's an prototype IB Cell): 我像这样初始化单元格(这是一个原型IB单元):
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as Item
//cell.textLabel.text = object.valueForKey("name")!.description
let cSubView = cell.viewWithTag(100) as RoundedIcon
cSubView.setUpViewWithFirstLetter(String(first(object.name)!).uppercaseString)
}
And the RoundedIcon works like this: 而RoundedIcon的工作方式如下:
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.cornerRadius = self.frame.size.width / 2;
self.layer.borderWidth = 1.0;
self.layer.borderColor = UIColor.lightGrayColor().CGColor;
self.clipsToBounds = true;
}
func setUpViewWithFirstLetter(letter:String){
self.backgroundColor = RoundedIcon.UIColorFromRGB(0x68C3A3)
let theLetterLabel = UILabel()
theLetterLabel.text = letter
theLetterLabel.textColor = UIColor.whiteColor()
theLetterLabel.textAlignment = .Center
theLetterLabel.font = UIFont.systemFontOfSize(25)
self.addSubview(theLetterLabel)
theLetterLabel.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: self.frame.size)
}
@rdelmar's comment pointed me in the right direction that an UITableview changes the background color of all it's cells to: @rdelmar的评论向我指出了一个正确的方向,即UITableview将其所有单元格的背景颜色更改为:
UIColor(white:0,alpha:0)
If you don't want your view to change it's color you should change the backgroundColor property setter, which works in swift like this: 如果您不想改变视图的颜色,则应更改backgroundColor属性设置器,该设置器的工作原理如下:
//override this setter to avoid color resetting on drag
override var backgroundColor:UIColor?{
didSet {
//check the color after setting - you also can do it earlier
if let bgCol = backgroundColor{
println(bgCol)
if bgCol == UIColor(white: 0, alpha: 0){ //check if it's settled by the table
super.backgroundColor = yourColor //set it back to your color
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.