简体   繁体   English

在表格单元格中动态设置标签值

[英]Set label value dynamically in a table cell

hello I am having a very weird problem in my code and I don't know whats actually going on here. 你好,我的代码中有一个非常奇怪的问题,我不知道这里真的发生了什么。

I have label set in my view controller and when I set the value like this 我在视图控制器中设置了label ,当我设置这样的值时

cell.totalTripsLabel.text = "55" 

It works. 有用。

But If I try to set value from dictionary, 但是,如果我尝试从字典中设置值,

cell.totalTripsLabel.text = self.dict["totalTrips"]! as? String

It doesn't work at all and nothing displays on ViewController. 它根本不起作用,并且在ViewController上没有任何显示。

If I print the value like this 如果我打印这样的值

print(self.dict["totalTrips"]!)

It successfully prints the integer value 成功打印出整数值

But If I do this 但是,如果我这样做

print(self.dict["totalTrips"]! as? String)

It prints nil 它打印nil

So I figure out by casting a value to string, it prints nil. 所以我通过将值转换为字符串来计算,它打印为nil。 So question is how I can set value in label which accepts string value 所以问题是我如何在接受字符串值的标签中设置值

Dictionary result is like this 字典结果是这样的

{
    totalTrips = 2;
}

尝试这个

cell.totalTripsLabel.text = String(self.dict["totalTrips"]!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM