【发布时间】:2021-02-01 12:52:48
【问题描述】:
我有一个表格视图,它从 api 请求中获取数据,从该请求中它只显示第一个结果。
//MARK:-- Computed vars
var listOfLodges = [LodgeDetail](){
didSet{
DispatchQueue.main.async {
self.tableView.reloadData()
self.navigationItem.title = "\(self.listOfLodges.count) lodges found"
print("\(self.listOfLodges.count) lodges found") // returns 11 lodges
print(" ")
}
}
}
这里显示11个返回结果,所有数据都不一样。
//MARK: -- ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
//MARK:-- Setting up Table view
@objc func setDelegate(){
let landing = LandingViewController()
landing.delegate = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return listOfLodges.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
这可能是我的错误所在,我不确定可能出了什么问题?
// possible errors here
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath)
let lodge = listOfLodges[indexPath.row]
let rating: String = String(lodge.rating!)//careful force unwrapping
print(rating) // prints the same 4.2
print(lodge.name) //prints the same name
return cell
}
}
//MARK:-- Functions called on did load
extension ListViewController{
func loadData(){
let lodgeRequest = LodgeRequest(lat:passedLat, long:passedLong)
lodgeRequest.getLodges{[weak self] result in
switch result{
case.failure(let error):
print(error)
case.success(let lodges):
self?.listOfLodges = lodges
}
}
}
}
【问题讨论】:
标签: ios json swift uitableview