Mysql 在UITableView中显示JSON中的数据
徖。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 我得到数据并打印出来。也许代码不正确,我只是swift新手,但我在Xcode中没有任何错误。有些东西不见了,但我不知道是什么。 导入UIKitMysql 在UITableView中显示JSON中的数据,mysql,json,swift,xcode,uitableview,Mysql,Json,Swift,Xcode,Uitableview,徖。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 我得到数据并打印出来。也许代码不正确,我只是swift新手,但我在Xcode中没有任何错误。有些东西不见了,但我不知道是什么。 导入UIKit class ViewCo
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
struct Class: Codable {
let first_name: String
let last_name: String
let email: String
enum CodingKeys: String, CodingKey {
case first_name = "first_name"
case last_name = "last_name"
case email = "email"
}
}
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://x.de/x.php")
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else { print(error!); return }
let decoder = JSONDecoder()
let classes = try! decoder.decode([Class].self, from: data)
for myClass in classes {
print(myClass.first_name)
print(myClass.last_name)
print(myClass.email)
}
}
).resume()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableview.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return myarray.count
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell", for: indexPath) as UITableViewCell
return cell
}
}
几个问题
- 视图控制器缺少数据源,该数据源是一个包含用户列表的数组
- 接收到远程数据后更新阵列(数据源)
- 数组(数据源)更改后,使用
tableView.reloadData()重新加载
UITableView
- 在
中返回数组元素的数量numberofrowsinssection
dataSource.count
要求您使用UITableView.dequeueReusableCell(标识符:for:)
方法注册一个单元格类或xibtableview.register
- 最后,使用camelCase变量命名约定作为firstName,而不是下划线作为firstName
以上代码生成
在cellForRowAt func中,您必须执行以下操作:
cell.textLabel?.text = myarray[indexPath.row]
因为myarray的文本应该显示在标签上或其他任何位置我只有一个错误,那就是:使用未解析的标识符'User'@AamirRCopy
struct User:Codable{…}
根据我上面的回答,应该解决这个问题,谢谢,我应该看到了,对不起。当我启动应用程序时,它崩溃了,我得到了一个错误:“线程1:致命错误:在解包可选值时意外地发现了nil”,代码是:override func viewdide(uu动画:Bool){super.viewdide出现(动画)tableview.reloadData()}@AamirRHi,感谢您的回答和帮助,它现在可以工作了:)。我在你的答案中编辑了我的url,所以没有人能看到它。只有一个问题,我试图显示firstName、lastName和email,但是不行,你能帮我吗?“myarray”我应该从代码中写什么@丹尼斯·费尔德布希
cell.textLabel?.text = myarray[indexPath.row]