2014-11-01 54 views
0

我有一个具有动态子视图的UITableView。当tableview改变时,单元格中的子视图会丢失颜色

当表是静态的,它看起来像这样: enter image description here 与T的全方位视角是自定义子视图

但是,当我选择编辑,然后拖动表格单元格中就失去它的颜色和T.

enter image description here

请告诉我这样做的原因是什么?

我初始化这样的电池(这是一个原型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) 
    } 

而且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) 
    } 
+0

你能从视图导航器中显示tableView的视图层次结构吗? – Spectravideo328 2014-11-01 15:22:49

+1

我认为这只是拖动过程中单元格的正常行为。你实际上并没有失去“T”,但你看不到它,因为它是白色的。如果您使用其他颜色,则会在拖动过程中看到它。一个正常的标签(没有任何修改的背景颜色)在拖动过程中也会丢失颜色。 – rdelmar 2014-11-01 16:11:14

+0

@rdelmar你是对的。该标签保持原状。那么它失去背景色的原因是什么? – arnoapp 2014-11-01 18:21:45

回答

1

@ rdelmar的评论我指出了正确的方向,一个UITableview将其所有单元的背景颜色更改为:

UIColor(white:0,alpha:0) 

如果您不希望你的视图改变它的颜色,你应该改变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 
      } 
     } 
    } 
} 
相关问题