gpt4 book ai didi

swift - 普通 View Controller 中的 UITableView 未使用我的自定义单元格进行更新

转载 作者:行者123 更新时间:2023-11-30 12:30:53 24 4
gpt4 key购买 nike

我在普通 View Controller 中放置了一个 TableView,它显示了我从 JSON 中提取的一些简单数据。单元格只有标签。在我的 ViewController 类中,我有以下代码,但我的 TableView 仍然不显示数据:

import UIKit

class EmployeePortal: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var dateLabel: UILabel!

var appData:Array<Dictionary<String,String>>? = nil
var numberOfApps:Int = 0

override func viewDidLoad() {
self.tableView.dataSource = self
self.tableView.delegate = self

let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
let date = dateFormatter.string(from: currentDate)
dateLabel.text = date

/////Methods to get data....////

do{
let JSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! Array<Dictionary<String,String>>
self.appData = JSON
print(self.appData) ///test just to make sure data is coming in
self.numberOfApps = JSON.count

//JSON file contains everything needed to make appointments
}catch{
print("ERROR DOWNLOADING JSON")
}
}
task.resume()
///I think my problem might be here, not sure how to make the table
//actually load the data
self.tableView.reloadData()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func numberOfSections(in tableView: UITableView) -> Int {
// There is only one section
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//The number of rows is equal to the amount of appointments the data returned:
return self.numberOfApps
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "appCell", for: indexPath) as! appCell //made sure to use my custom cell
let row = indexPath.row
cell.patientName.text = self.appData?[row]["pName"]
cell.reasonLabel.text = self.appData?[row]["reason"]
cell.dateLabel.text = self.appData?[row]["date"]
return cell //do all the stuff to update the labels and return it
}


}

不知道我还缺少什么。我已正确设置 TableView 的委托(delegate)和数据源。我想我缺少一些小东西,但我将不胜感激任何帮助!

最佳答案

DispatchQueue.main.async - 用于更新 UI,因为网络调用是后台队列。

试试这个方法

 do{
let JSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! Array<Dictionary<String,String>>
self.appData = JSON
print(self.appData) ///test just to make sure data is coming in
self.numberOfApps = JSON.count
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
//JSON file contains everything needed to make appointments
}
catch{
print("ERROR DOWNLOADING JSON")
}

更新:-

if let pName =  (self.appData[row]["pName"]) as? String {
cell.patientName.text = pName
}

关于swift - 普通 View Controller 中的 UITableView 未使用我的自定义单元格进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573470/

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