Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 基于Tableview数据更改的Swift Tableview单元格按钮图像_Ios_Json_Swift_Tableview - Fatal编程技术网

Ios 基于Tableview数据更改的Swift Tableview单元格按钮图像

Ios 基于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

我试图解析JSON并在tableview数组中追加JSON响应值。下面是response
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,否则为其值

  • 应用到单元格后,如果值为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}