如何在Swift中将数据从外部数据库填充到UItableView

我试图使用PHP脚本从外部数据库填充数据,我可以看到已在viewdidload()收到数据。 但是我无法在tableview中看到数据。

我尝试使用断点,但没有运气,因为我的程序无法达到

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ....} 

你能告诉我我做得不好吗?

 import UIKit class UsersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var data: NSArray = [] @IBOutlet weak var listTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() data = dataOfJson(url: "http://kpstudio18.com/app/select.php") print("data are as \(data)") } func dataOfJson(url: String) -> NSArray { var data = NSData(contentsOf: NSURL(string: url)! as URL) var error: NSError? var jsonArray: NSArray = try! JSONSerialization.jsonObject(with: data! as Data, options: .mutableContainers) as! NSArray return jsonArray } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if data.count != 0 { return data.count } else { return 1 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: additionInfoCell = self.listTableView.dequeueReusableCell(withIdentifier: "customCell") as! additionInfoCell let maindata = (data[indexPath.row] as! NSDictionary) cell.name.text = maindata["name"] as? String cell.info.text = maindata["id"] as? String print("Cell Name is \(cell.name.text)") print("Cell Info (id) is \(cell.info.text)") return cell } } 

 yourtableview_name.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell") 

viewdidload尝试这个。如果有效,请告诉我。

你只是忘了重装你的桌子。 在viewDidLoad()方法中添加以下行yourTableView.reloadData() ,希望它能工作。

另外,检查您是否通过RB1509注册了上述“CustomCell”