为什么本地路径中的JSON数据在UITableView中显示不正确?

时间:2017-09-08 19:22:40

标签: swift uitableview

我想解析项目中可用的本地文件中的JSON数据,然后将这些数据填充到UITableView。

我的要求

  1. 解析本地路径中的json数据而非URL
  2. 将json数据填充到UITableView
  3. 遇到问题

    1. 无法显示已解析的数据,(括号显示在表格视图中。
    2. 我可以使用dump()在控制台中打印数据,但无法在tableView
    3. 中打印数据

      更新了视图控制器,用于将数据传递到另一个控制器

          func numberOfSections(in tableView: UITableView) -> Int {
                  return 1
              }
      
              func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
                  return lookArrayModel.count
              }
              func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
                  let cells = myTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
      
                  let displayData = lookArrayModel[indexPath.row]
      
       cells.textLabel?.text = String(describing: displayData.Lookname!)
                  cells.detailTextLabel?.text = String(describing: displayData.Lookdetails!)
      
                //  print(displayData.shadeModel)
                   return cells
              }
      
              func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
      
                  print("You selected cell #\(indexPath.row)!")
      
                  // Get Cell Label
      
                  let indexPath = myTableView.indexPathForSelectedRow;
                  let currentCell = myTableView.cellForRow(at: indexPath!) as UITableViewCell!;
      
                   lookNameValue = currentCell?.textLabel?.text
      
                  lookDetailValue = currentCell?.detailTextLabel?.text
             }
              override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      
                  //let lookShade = LookModelData()
                  if (segue.identifier == "segueToLook") {
      
        let destController:DetailsViewController = segue.destination as! DetailsViewController
      
                    //Set the selecte row index value
                      destController.LabelText = String(describing: lookNameValue)
                      destController.DetailText = String(describing: lookDetailValue)
      
                    //  destController.arrayData = lookShade.shadeModel as! NSMutableArray
      
                  }
              }
          }
      

      目标视图控制器。斯威夫特

      class DetailsViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
      
                  var lookArrayModel = [LookModelData]()
                  var arrayData: NSMutableArray = []
      
                  @IBOutlet weak var secondView: UITableView!
                  var LabelText = String()
                  var DetailText = String()
                  var shadeText = String()
                  @IBOutlet weak var LookLabel: UILabel!
                  @IBOutlet weak var LookName: UILabel!
      
      
                  override func viewDidLoad() {
                      super.viewDidLoad()
      
                      // Do any additional setup after loading the view.
                      print(arrayData)
      
                      LookName?.text = LabelText
      
                      LookLabel?.text = DetailText
      
                     secondView.dataSource = self
                      secondView.delegate = self
                      secondView.reloadData()
                  }
                  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
                      return arrayData.count
      
                  }
                  func numberOfSections(in tableView: UITableView) -> Int {
      
                      return 1
                  }
                  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                      let cells = secondView.dequeueReusableCell(withIdentifier: "secondCell", for: indexPath)
      
      
                      let displayData = arrayData
      
                  //    cells.textLabel?.text = String(describing: (displayData as AnyObject))
                    //    print(arrayData)
      
      
                      return cells
                  }  
      
          }
      

1 个答案:

答案 0 :(得分:1)

请检查我的代码:

将lookArrayModel类型NSMutableArray更改为[LookModelData]。像那些我做了一些改变。请检查。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
     var lookArrayModel = [LookModelData]()

    @IBOutlet weak var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        guard let Path = Bundle.main.path(forResource: "ColorShade", ofType: "json") else { return }

        let url = URL(fileURLWithPath: Path)

        do {
            let data = try Data(contentsOf: url)
            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

            myTableView.dataSource = self
            myTableView.delegate = self

            //Calling the function for adding look
            createLooks(dictionary: json as! NSArray)
            myTableView.reloadData()
        }  catch {
            print(error)
        }
    }

    func createLooks(dictionary:NSArray) {
        for item in dictionary {
            let item1 = item as! NSDictionary
            let lookModal = LookModelData()
            lookModal.Lookname = item1.value(forKey: "Lookname") as? String
            lookModal.LookId = item1.value(forKey: "LookId") as? String
            lookModal.Lookdetails = item1.value(forKey: "Lookdetails") as? String
            lookModal.shadeModel = createshade(shades: item1.value(forKey: "shades") as! NSArray)
            lookArrayModel.append(lookModal)
        }
    }

    func createshade(shades: NSArray) -> [ShadeDescription] {
        var arrayShade = [ShadeDescription]()
        for item in shades
        {
            let item1 = item as! NSDictionary
            let shadeModal = ShadeDescription()
            shadeModal.comboID = item1.value(forKey: "comboID") as? String
            shadeModal.shadeName = item1.value(forKey: "shadeName") as? String
            shadeModal.ShadeType = item1.value(forKey: "ShadeType") as? String
            shadeModal.ShadeCode = item1.value(forKey: "shadeCode") as? String
            arrayShade.append(shadeModal)
        }
        return arrayShade
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lookArrayModel.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cells = myTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let displayData = lookArrayModel[indexPath.row]

        // You will get like this 
        // print(displayData.LookId!)
        // print(displayData.Lookname!)
        // print(displayData.Lookdetails!)
        // print(displayData.shadeModel!)
        // This is the way to get shade model data
        if let shadeModels = displayData.shadeModel {
            for var shadeModel in shadeModels {
                print(shadeModel.comboID)
                print(shadeModel.ShadeType)
                print(shadeModel.shadeName)
                print(shadeModel.ShadeCode)
            }
        }
        cells.textLabel?.text = String(describing: displayData.Lookname!)

        return cells
    } 
}

class LookModelData
{
    var Lookname:String?
    var LookId:String?
    var Lookdetails:String?
    //Shades Array
    var shadeModel : [ShadeDescription]?
}

class ShadeDescription {
    var ShadeType:String?
    var shadeName:String?
    var comboID:String?
    var ShadeCode:String?
}