2016-09-17 3 views
2

tableViewCell에서 뷰의 배경색을 변경하려고하지만 첫 번째로드에서는 변경되지 않으며 변경 사항을 보려면 스크롤해야합니다. break이 스위치를 종료합니다 의미로,테이블 뷰 셀에서 뷰의 색상 변경이 이루어지지 않습니다.

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

당신은이 상황에서 브레이크를 사용해서는 안 :

@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의 색상을 변경하는 코드 : 이것은 내 세포 클래스 . 스위프트 3 문서에서

: C와 목표 - 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.status_back.backgroundColor)''return cell' 바로 전에? – pedrouan