как разобрать массив изображений из формата json в tabelview - PullRequest
0 голосов
/ 23 января 2019

Plea

привет, у меня есть данные json, как показано ниже

{
  "data" : [
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "test",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Test",
      "id" : "45",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_fzvXhnBwpw1547270966.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "test",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Ahmedabad",
      "id" : "47",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_UNQbzzwTaK1547616334.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "shhsge",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Shsg",
      "id" : "57",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "ewfewf",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "retested",
      "id" : "58",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_8edaM4H6lZ1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_KhDXUhCHRN1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_zIhztQ2mVD1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_I3RWeD0VKl1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_VuDzsyR5gI1548233123.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "dads sad",
      "id" : "59",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_n19NGjRfcH1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_WQn8zZeT0X1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_GYF21A9lMd1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_eaeoXVIPbU1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_w7KTfkTtdT1548233550.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "adds",
      "id" : "60",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_fVghhQFhB31548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_k3Y5CaQIsj1548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_nVhXpintD51548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_gS0wahnPzZ1548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_TT2RZF2Pnv1548233912.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "sad sad",
      "id" : "61",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_iy7hMFz69u1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_799ad8B4aj1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_5HFLk03Fgp1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_9zYgQKVqQ51548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_UYf49scKwn1548234019.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sdsadas",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "daddy’s sad sad",
      "id" : "62",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_htPOrErbgF1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Y4ms5mkzgD1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_nuFk2NsfzV1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_IRpxGQb72s1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_N0w9sCwNcM1548234575.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsa",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "asdsadas",
      "id" : "63",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_jJj0URkS9o1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_GU95bP9bhn1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Arob8rq0QU1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_79Erayiurr1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_T8m2dxSmI91548234685.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    },
    {
      "resolved" : "",
      "type" : "Internal",
      "notes" : "Test test",
      "property_id" : "22",
      "regulation_item" : "26",
      "locations" : "Kitchen",
      "id" : "11",
      "boiler_service_date" : "0000-00-00",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "",
      "regulation_id" : "6",
      "regulation_item_name" : "Gas",
      "contravention" : "202",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_j9QNiQ9Sun1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_zVBEjnkZf41548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_LTaxAfGTZW1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Mj5D6otfhX1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_DQTfucDObS1548225475.png"
      ],
      "contraventions_item_name" : "Condition and certs"
    },
    {
      "resolved" : "",
      "type" : "Internal",
      "notes" : "",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "6",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    },
    {
      "resolved" : "",
      "type" : "Internal",
      "notes" : "test test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "7",
      "boiler_service_date" : "",
      "action" : "Extractor To Be Vented Out \/ Mechanical Fan To Be Installed",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_1Ot5YN8xL11548151400.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    },
    {
      "resolved" : "",
      "type" : "Internal",
      "notes" : "test test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "8",
      "boiler_service_date" : "",
      "action" : "Extractor To Be Vented Out \/ Mechanical Fan To Be Installed",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_9vhD62hRcp1548151402.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    },
    {
      "resolved" : "",
      "type" : "Internal",
      "notes" : "Test Test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "9",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_SbrrQxWl2A1548151619.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_ly1adD9T3C1548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_qVup3MTUHh1548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_4Asw2j3YF61548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_y0FAbTekMh1548151620.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    }
  ],
  "total_count" : "1",
  "success" : "1",
  "message" : "Previous inspection list."
}

из этих данных я получаю данные array, но не могу отобразить наtableview а также я не могу понять, как получить массив изображений из этих данных. Вот мой код для заполнения моего tableview

Код

func PreviousInspeList(){
    let preferences = UserDefaults.standard
    let uid = "u_id"
    let acTkn = "acc_tkn"

    let u_ID = preferences.object(forKey: uid)
    let A_Token = preferences.object(forKey: acTkn)

    let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!]
    print(params)
    SVProgressHUD.show()
    Alamofire.request(previousinspectionslist, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
        switch response.result{
        case.success(let value):
            let json  = JSON(value)
            print(json)
            let data = json["data"]
            data.array?.forEach({ (iList) in
                let inspList = PreviousInspListModel(resolved: iList["resolved"].stringValue, type: iList["type"].stringValue, notes: iList["notes"].stringValue, property_id: iList["property_id"].stringValue, regulation_item: iList["regulation_item"].stringValue, locations: iList["locations"].stringValue, id: iList["id"].stringValue, boiler_service_date: iList["boiler_service_date"].stringValue, action: iList["action"].stringValue, intervention_required: iList["intervention_required"].stringValue, regulation_id: iList["regulation_id"].stringValue, regulation_item_name: iList["regulation_item_name"].stringValue, contravention: iList["contravention"].stringValue, contraventions_item_name: iList["contraventions_item_name"].stringValue)
                self.preInspData.append(inspList)
            })
            self.tblListView.reloadData()

            SVProgressHUD.dismiss()
        case.failure(let error):
            print(error.localizedDescription)
        }

    })
}

вот мой класс модели

struct PreviousInspListModel {
    var resolved: String
    var type: String
    var notes: String
    var property_id: String
    var regulation_item: String
    var locations: String
    var id: String
    var boiler_service_date: String
    var action: String
    var intervention_required: String
    var regulation_id: String
    var regulation_item_name: String
    var contravention: String
    var contraventions_item_name: String
}

Я просто хочу сказать, почему мои данные не заполняются на tableview и как получить изображения array, а также отображаться на tableview, пожалуйста, помогите мне в этом

Ответы [ 2 ]

0 голосов
/ 24 января 2019

Swift 4, Apple представила совершенно новый способ кодирования и декодирования данных протоколов, сопоставляя ваши пользовательские типы с некоторыми простыми в использовании протоколами

1) Кодируемые - для кодирования

2) Декодируемый - для декодирования

3) Кодируемый - как для кодирования, так и для декодирования

Он также поддерживает класс, структуру и перечисление.

В соответствии с протоколом Codable вы должны создать класс модели в качестве формата ответа, не нужно анализировать данные вручную.

В вашем примере:

struct ResponseData : Codable {
    let message: String
    let data : [PreviousInspListModel]
}

struct PreviousInspListModel : Codable {
    let resolved: String?
    let notes : String
    let type: String
    let images: [String]
}

В вашемФункция ответа API:

Alamofire.request(previousinspectionslist, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseData { (responseData) in
            responseData.result.ifSuccess {
                if let data = responseData.data{
                    do {
                        let response = try JSONDecoder().decode(ResponseData.self, from: data) //Decode JSON Response Data
                        print(response)
                    } catch let parsingError {
                        print("Error", parsingError)
                    }
                }
            }
        }

Вы можете найти более подробную информацию о swift 4 codable или просто выполнить поиск по термину в Интернете.

0 голосов
/ 23 января 2019

Добавьте UICollectionView внутри UITableViewCell, чтобы показать все изображения.и используйте массив изображений для заполнения UICollectionView.найдите отличный учебник для добавления UICollectionView внутри UITableView ячейки

https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

struct PreviousInspListModel {
    var resolved: String
    var type: String
    var notes: String
    var property_id: String
    var regulation_item: String
    var locations: String
    var id: String
    var boiler_service_date: String
    var action: String
    var intervention_required: String
    var regulation_id: String
    var regulation_item_name: String
    var contravention: String
    var contraventions_item_name: String
    var imageArray: [String]!
}

в вашем цикле:

data.array?.forEach({ (iList) in
                let inspList = PreviousInspListModel(resolved: iList["resolved"].stringValue, type: iList["type"].stringValue, notes: iList["notes"].stringValue, property_id: iList["property_id"].stringValue, regulation_item: iList["regulation_item"].stringValue, locations: iList["locations"].stringValue, id: iList["id"].stringValue, boiler_service_date: iList["boiler_service_date"].stringValue, action: iList["action"].stringValue, intervention_required: iList["intervention_required"].stringValue, regulation_id: iList["regulation_id"].stringValue, regulation_item_name: iList["regulation_item_name"].stringValue, contravention: iList["contravention"].stringValue, contraventions_item_name: iList["contraventions_item_name"].stringValue, imageArray: iList["images"].arrayObject as? [String])
                self.preInspData.append(inspList)
            })
...