我正在尝试使用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
}
}
2 回答
试试这个
viewdidload
. 如果有效,请告诉我 .你只是忘了重装你的 table . 在viewDidLoad()方法中添加以下行
yourTableView.reloadData()
,希望它可以正常工作 .另外,检查您是否通过RB1509注册了上述“CustomCell”