gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:20:20 26 4
gpt4 key购买 nike

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

我的要求

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

面对问题

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

更新了 View Controller ,用于将数据传递到另一个 Controller 。

    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

}
}
}

目标 View Controller 。 swift

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
}

}

最佳答案

请检查我的代码:

将 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?
}

关于swift - 为什么来自本地路径的 JSON 数据在 UITableView 中显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46123298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com