2016-09-17 33 views
2

ビューの背景色をtableViewCellに変更していますが、最初のロード時に変更されず、スクロールして変更を確認する必要があります。これは私のセルクラスです:テーブルビューのセル内のビューの色を変更できません

@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 
} 

とのViewControllerにstatus_backの色を変更するコード:

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はそれがスイッチから出るよ手段としてあなたは、このような状況でブレークを使用してはいけません。スウィフト3つのドキュメントから

:CとObjective-Cでswitch文とは対照的に

、スイフトに 文スイッチは、デフォルトで、次のいずれかに各ケースの底面と を通じて該当しません。代わりに、switch文全体 は、明示的なbreak文を必要とせずに、最初に一致するスイッチのケースが完了するとすぐにその実行を終了します( )。これにより、 スイッチのステートメントが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(cell.status_back.backgroundColor)' 'return cell'の直前ですか? – pedrouan

関連する問題