[英]Change Text Color of Label in cell with if statement
如果AlarmLevel!=“ 0”的值,我试图更改单元格中一个或多个标签的颜色。 到目前为止,这就是我要的,它正在更改所有单元格中cell.locationLabel的颜色,而不仅仅是满足if语句的颜色。 我确定它与indexPath有关,但我无法弄清楚。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ViewControllerTableViewCell
cell.locationLabel.text = eventsList[indexPath.row].location
cell.eventTimeLabel.text = eventsList[indexPath.row].dateTime
cell.eventTypeLabel.text = eventsList[indexPath.row].agencyEventSubtypeCode
cell.agencyIdLabel.text = eventsList[indexPath.row].agencyId
if alarmLevel != "0" {
cell.locationLabel.textColor = UIColor.red
} else {
cell.locationLabel.textColor = UIColor.black
}
return cell
}
您需要将alarmLevel
作为整数变量添加到事件列表数组类型。 并根据需要对数组eventsList
中的每个event
项更改此整数值
class Events {
var location = String()
var dateTime = String()
var agencyEventSubtypeCode = Int()
var agencyId = Int()
var alarmLevel = Int() //Need this here
}
var eventsList = [Events]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ViewControllerTableViewCell
cell.locationLabel.text = eventsList[indexPath.row].location
cell.eventTimeLabel.text = eventsList[indexPath.row].dateTime
cell.eventTypeLabel.text = ("\(eventsList[indexPath.row].agencyEventSubtypeCode)")
cell.agencyIdLabel.text = ("\(eventsList[indexPath.row].agencyId)")
if eventsList[indexPath.row].alarmLevel != 0 {
cell.locationLabel.textColor = UIColor.red
} else {
cell.locationLabel.textColor = UIColor.black
}
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.