Ios 基于Tableview数据更改的Swift Tableview单元格按钮图像
我试图解析JSON并在tableview数组中追加JSON响应值。下面是responseIos 基于Tableview数据更改的Swift Tableview单元格按钮图像,ios,json,swift,tableview,Ios,Json,Swift,Tableview,我试图解析JSON并在tableview数组中追加JSON响应值。下面是responsesubcategory我得到的一些对象的值,但其中一些对象的值为null { "category": [ { "id": 0, "subcategory": [ //Table cell button need to show green color { "subcategory_id": 10 } ] }, { "i
subcategory
我得到的一些对象的值,但其中一些对象的值为null
{
"category": [
{
"id": 0,
"subcategory": [ //Table cell button need to show green color
{
"subcategory_id": 10
}
]
},
{
"id": 0,
"subcategory": null //Table cell button need to show red color
}
]
}
中,比如:[10,null,…]
。如果子类别为null,则表示需要存储null,否则为其值 if indexPath.row < id.count {
cell.accessibilityValue = String(id[indexPath.row]) // If the cell.accessibilityValue not null then need to change cell button image.
}
cell.name_Label.text = name[indexPath.row]
cell.city_Label.text = city[indexPath.row]
如果indexath.row
从JSON追加数组self.id.append(items)
。我得到的输出像[10]一样,但实际结果应该是[10,null]
。数组的长度不正确,如果数据为null或nil,我需要数据为“null”,因为我通过索引获取值,每个索引都知道为null或有值,但它必须存在
id
应该是可选Int
子类别\u id
应为可选Int
var subcategory_id: Int?
var id: [Int?] = []
if let subCat = subcategory_id {
id.append(subCat)
}
else {
id.append(nil) // id=[nil]
}
在你的牢房里
id
是一个可选Int
(Int?
)数组,您必须将其展开
cell.id_Label.text = id[indexPath.row]!
或
if let i = id[indexPath.row] {
cell.id_Label.text = "\(i)"
}
else {
print("id is nil")
}
是否已将项目附加到数组中?在这里打印你的数组更新后请检查@Shahzaibqureshie数组中的每个项都是字符串?或者dictionary?它的字符串不是dictionary@ShahzaibQureshithen您可以这样检查:if id[indexath.row]==null{//setImage}else{},但当我为单元格标签赋值时,获取cell.id_label.text=String(id[indexath.row])错误:无法使用类型为“(Int”)的参数列表调用类型为“String”的初始值设定项“@Hamed akhlaghi使用此“\(id[indexPath.row])”而不是字符串(id[indexPath.row]),如果id没有值,则此代码将添加nil到cell.id\u Label.text,这很好。但我得到的结果是可选的(值),也就是cell.id_Label.text=String(((id[indexPath.row]),警告字符串插值生成可选值的调试描述;你的意思是要说清楚吗@Hamed Akhlaghiif(id[indexath.row])!=nil{cell.id_Label.text=“(id[indexPath.row])!”}否则{cell.id_Label.text=“}您应该展开(id[indexPath.row])或设置默认值如果(id[indexPath.row]),则无法获得相同的选项@Hamed Akhlaghinil{cell.id_Label.text=String(((id[indexath.row]))cell.id_Label.textColor=UIColor.red}其他{cell.id_Label.text=”“cell.id_Label.textColor=UIColor.green}