[英]Change Text Color of Label in cell with if statement
I'm trying to change the color of a label (or labels) in a cell if the value of alarmLevel != "0". 如果AlarmLevel!=“ 0”的值,我试图更改单元格中一个或多个标签的颜色。 This is what I have so far and it's changing the color of the cell.locationLabel in all of the cells instead of just the ones that satisfy the if statement.
到目前为止,这就是我要的,它正在更改所有单元格中cell.locationLabel的颜色,而不仅仅是满足if语句的颜色。 I'm sure its something to do with the indexPath but I cant figure it out.
我确定它与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
}
You need to add the alarmLevel
as an integer variable to the events list array type. 您需要将
alarmLevel
作为整数变量添加到事件列表数组类型。 And change this integer value according to your need for each event
item in the array eventsList
并根据需要对数组
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.