gpt4 book ai didi

ios - 在 tableview 单元格中更改 View 的颜色不会改变

转载 作者:可可西里 更新时间:2023-11-01 02:14:29 25 4
gpt4 key购买 nike

我正在更改 tableViewCell 中 View 的背景颜色,但它在第一次加载时不会更改,我需要滚动才能看到更改。这是我的单元格类:

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

最佳答案

在这种情况下你不能使用 break,因为 break 意味着它将退出开关。

来自 Swift 3 文档:

In contrast with switch statements in C and Objective-C, switch statements in Swift do not fall through the bottom of each case and into the next one by default. Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. This makes the switch statement safer and easier to use than the one in C and avoids executing more than one switch case by mistake.

在 Swift 中,切换控制流程在满足特定情况后立即结束

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

关于ios - 在 tableview 单元格中更改 View 的颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com