2016-09-17 66 views
2

我正在更改tableViewCell中某個視圖的背景顏色,但它在第一次加載時不會更改,我需要滾動才能看到更改。這是我的手機類:在tableview單元格中更改視圖的顏色不會更改

@IBOutlet weak var status_back: UIView! 
@IBOutlet weak var status_label: UILabel! 
@IBOutlet weak var node_label: UILabel! 
@IBOutlet weak var time_label: UILabel! 
@IBOutlet weak var name_label: UILabel! 
@IBOutlet weak var res_date: UILabel! 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

override func layoutSubviews() { 
    status_back.layer.cornerRadius = status_back.frame.height/2 
} 

和改變status_back的顏色的ViewController代碼:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cellIdentifier = "MealCell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ReservationCell 
    let reservation = filteredreservations[(indexPath as NSIndexPath).row] 
    cell.name_label.text = "نام: "+reservation.client_name 
    cell.name_label.font = UIFont(name: "WeblogmaYekan", size: 17) 
    cell.time_label.text="زمان: "+reservation.ft_of_time+"-"+reservation.ft_to_time 
    cell.time_label.font = UIFont(name: "B Yekan", size: 17) 
    cell.res_date.text="تاریخ: \(reservation.date)" 
    cell.res_date.font = UIFont(name: "B Yekan", size: 17) 
    var status = "" 
    if reservation.type { 
     cell.node_label.text="ex1 \(reservation.node_title)" 
    } else { 
     cell.node_label.text="ex2" 
    } 
    switch reservation.res_status { 
    case "CanceledByAdmin": 
     cell.status_back.backgroundColor=Utils.UIColorFromRGB(rgbValue: 0xFFFC635D) 
     status = "ex" 
    case "Canceled": 
     cell.status_back.backgroundColor=Utils.UIColorFromRGB(rgbValue: 0xFFEE903D) 
     status = "ex" 
    case "Deprecated": 
     cell.status_back.backgroundColor=Utils.UIColorFromRGB(rgbValue: 0xFF757575) 
     status = "ex" 
    default: 
     cell.status_back.backgroundColor=Utils.UIColorFromRGB(rgbValue: 0xFF3BA757) 
     status = "ex" 
    } 
    cell.status_label.text=status 
    cell.status_label.font = UIFont(name: "WeblogmaYekan", size: 17) 
    return cell 
} 
+0

是'cellForRowAtIndexPath'裏面的代碼? –

+0

是的,我確定@NiravD –

回答

0

我找到了答案。創建小區時,有用於status_back.frame.height沒有數據,所以

override func layoutSubviews() { 
    status_back.layer.cornerRadius = status_back.frame.height/2 
} 

使得status_back無形的,在下一次循環代碼工作

1

你千萬不要在這種情況下使用break,因爲break意味着它會從交換機退出。

從夫特3文檔:

在與C和Objective-C switch語句相反,切換在夫特 語句不通過每個殼體的底部和 落入由默認下一個。相反,只要完成第一個匹配開關案例 ,整個開關語句 就會結束其執行,而不需要明確的中斷語句。這使得 開關語句比C中的開關語句更安全和更易於使用,並且避免錯誤地執行多個開關情況。

開關控制流結束某些情況下,在滿足斯威夫特後右。

switch reservation.res_status { 
    case "CanceledByAdmin": 
     color = Utils.UIColorFromRGB(rgbValue: 0xFFFC635D) 
     status = "example4"   
    case "Canceled": 
     color = Utils.UIColorFromRGB(rgbValue: 0xFFEE903D) 
     status = "example3" 
    case "Deprecated": 
     color = Utils.UIColorFromRGB(rgbValue: 0xFF757575) 
     status = "example2" 
    default: 
     color = Utils.UIColorFromRGB(rgbValue: 0xFF3BA757) 
     status = "example" 
    } 

    cell.status_label.text=status 
    cell.status_back.backgroundColor = color 
    cell.status_label.font = UIFont(name: "WeblogmaYekan", size: 17) 
    return cell 
+0

它結束於目前遇到的情況。你可以在這裏閱讀所有的文檔https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID139 – pedrouan

+0

感謝提及但它不能解決我的問題 –

+0

不客氣。我需要提到這一點,因爲我不確定這是否是問題。我沒有結束。我想幫助你解決這個問題。你可以發佈,輸出這個:'print(cell.status_back.backgroundColor)''在'return cell'之前? – pedrouan