更改tableview单元格中视图的颜色不会进行更改

时间:2016-09-17 11:02:57

标签: ios swift uitableview tableviewcell

我在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
}

2 个答案:

答案 0 :(得分:1)

在这种情况下,您不能使用break,因为break表示它会从交换机退出。

来自Swift 3文档:

  

与C和Objective-C中的switch语句相比,切换   Swift中的语句不会落在每个案例的底部   默认情况下进入下一个。相反,整个switch语句   一旦第一个匹配的开关盒就完成它的执行   完成后,无需明确的中断声明。这使得   switch语句比C语言更安全,更容易使用   避免误操作多个开关盒。

开关控制流在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

答案 1 :(得分:0)

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

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

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