Ios 根据来自服务器的文本更改标签背景颜色
我想改变标签的背景颜色,根据来自服务器的文本,如果发送(文本)来。背景色为红色,其他颜色为绿色 我的for循环工作正常。但所有标签上的颜色都是绿色 (类型)是一个全局清除变量Ios 根据来自服务器的文本更改标签背景颜色,ios,swift,uitableview,for-loop,uilabel,Ios,Swift,Uitableview,For Loop,Uilabel,我想改变标签的背景颜色,根据来自服务器的文本,如果发送(文本)来。背景色为红色,其他颜色为绿色 我的for循环工作正常。但所有标签上的颜色都是绿色 (类型)是一个全局清除变量 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifi
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! HomeCell
for all in (arrayForType)!
{
print(all)
let types = (all as AnyObject).object(forKey: "type") as! String
print(types)
if types == "Send"
{
cell.lblForSend.backgroundColor = UIColor.red
}
else
{
cell.lblForSend.backgroundColor = UIColor.green
}
}
return cell
}
在我看来,问题是因为在循环中,将背景颜色更改为红色后,您会得到另一个文本,它不是
Send
,它会使您的标签恢复为绿色
你可以试试我下面的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! HomeCell
let types = (arrayForType[indexPath.row] as AnyObject).object(forKey: "type") as! String
if types == "Send"
{
cell.lblForSend.backgroundColor = UIColor.red
} else {
cell.lblForSend.backgroundColor = UIColor.green
}
return cell
}
在我看来,问题是因为在循环中,将背景颜色更改为红色后,您会得到另一个文本,它不是
Send
,它会使您的标签恢复为绿色
你可以试试我下面的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! HomeCell
let types = (arrayForType[indexPath.row] as AnyObject).object(forKey: "type") as! String
if types == "Send"
{
cell.lblForSend.backgroundColor = UIColor.red
} else {
cell.lblForSend.backgroundColor = UIColor.green
}
return cell
}
调用
print(types)
时记录了什么?它是一步一步打印的,并且根据文本的条件执行if和else。在这种情况下,所有标签都应为红色。对吗?是的,对。你有什么建议吗?你能把print(all)
的输出贴出来吗?或者整个arrayForType
?调用print(types)
时记录了什么?它是一步一步打印的,并且根据文本的条件执行if和else。在这种情况下,所有标签都应为红色。对吗?是的,对。你有什么建议吗?你能把print(all)
的输出贴出来吗?或者整个arrayForType
?这不起作用所有文本的背景色都显示为sameI要求所有标签都应为红色,你说是吗?阅读关于问题的评论哦。。。很抱歉,实际上我想根据文本进行更改。如果发送显示在标签上,颜色将为红色,如果接收显示在标签上,则颜色将为绿色。这不起作用所有文本的背景颜色都显示sameI要求所有标签都应为红色,您是否同意?阅读关于问题的评论哦。。。很抱歉,实际上我想根据文本进行更改。如果发送显示在标签上,则颜色将为红色,如果接收显示在标签上,则颜色将为绿色。