在 tableview 单元格中更改视图的颜色不会改变

Changing color of a view in tableview cell doesn't make change

我正在更改 tableViewCell 中视图的背景颜色,但它在第一次加载时不会更改,我需要滚动才能看到更改。这是我的手机 class:

@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意味着它会退出switch。

来自 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

我找到了答案。创建单元格时 status_back.frame.height 没有数据,因此

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

使 status_back 不可见,下次它回收代码时有效