2016-07-20 35 views
0

我有一個ViewController和一個TableView。
通過代碼,我填充TableView的單元格和其他代碼,我設置單元格的BackgroundColor。
注 - 在代碼的其他部分我這樣做
cell.backgroundColor = UIColorFromRGB(0xAA66CC) //雖然每個單元格的具體RGB值會有所不同。iOS Swift - 從選定的TableView中獲取BackgroundColor單元

現在在我的Segue到另一個視圖我想檢索,不僅「名稱」元素,而且單元格的BackgroundColor並將其發送到下一個ViewController。
我得到的「名稱」很好,但我不知道如何獲得BackgroundColor值(我將需要轉換爲字符串傳遞)。

我Segue公司代碼:

override func prepareForSegue(segue: UIStoryboardSeque, sender: AnyObject?) 
{ 
if segue.identifier = "GoToNextVC" { 
    if let cell = sender as? UITableViewCell { 
    let destination = segue.destinationViewController as! NextVC 
    let indx = tableView.IndexPathForCell(cell).row 
    destination.SelectedValue = self.peripherals[indx].name // This works fine. 

    destination.SelectedColor = **< I don't know what to do here >** 
    } 
    } 
} 

該代碼工作得到SelectedObject名稱,但我不知道怎麼去Cell的BACKGROUNDCOLOR這樣我就可以再通過Segue公司傳遞。

您的協助將不勝感激。

+0

不是我已經嘗試過,但有明顯的錯誤:'cell.backgroundColor'? –

+0

嘗試cell.contentView.backgroundColor –

回答

0

我會嘗試:

cell.backgroundColor 

如果你有電池,那麼你可以得到的顏色。

override func prepareForSegue(segue: UIStoryboardSeque, sender: AnyObject?) 
{ 
    if segue.identifier = "GoToNextVC" { 
    if let cell = sender as? UITableViewCell { 
     let destination = segue.destinationViewController as! NextVC 
     let indx = tableView.IndexPathForCell(cell).row 
     destination.SelectedValue = self.peripherals[indx].name // This works fine. 

     destination.SelectedColor = cell.backgroundColor 
    } 
    } 
} 
+0

感謝您的答覆。我發現:1)** cell.backgroundColor!**給出了UIColor 2)AND ** cell.contentView.backgroundColor!**也給了我UIColor - 我不確定它們有什麼區別,但它是一個開始。 – Dhugalmac

+0

我有一個將UIColor轉換爲RGBA的例程,但現在我需要將RGBA值合併爲一個HEX字符串。有什麼建議麼? – Dhugalmac

相關問題