【问题标题】:Why is JSON data from local path shows incorrectly in the UITableView?为什么本地路径的 JSON 数据在 UITableView 中显示不正确?
【发布时间】:2017-09-08 19:22:40
【问题描述】:

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

我的要求

  1. 从本地路径而不是从 URL 解析 json 数据
  2. 将json数据填充到UITableView

面临的问题

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

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

    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
            }  

    }

【问题讨论】:

    标签: swift uitableview


    【解决方案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?
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-29
      • 2014-03-23
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多