2014-11-01 47 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 
      } 
     } 
    } 
} 
相關問題