我正在使用Alamofire
和SwiftyJSOn
调用api,并且我正在创建用于在Tableview中显示数据的模型类,这是我的回应
回复
{
"inspections_todays_data" : [
],
"message" : "Successfully.",
"success" : "1",
"inspections_future_data" : [
],
"inspections_overdue_data" : [
{
"notes" : "Test",
"surveyor_id" : "8",
"longitude" : null,
"time" : "08:00",
"address3" : null,
"county" : "carlow",
"client_id" : "3",
"house_num" : "test street",
"eircode" : "12345",
"address1" : "test area",
"date_inspected" : "2019-01-10",
"address2" : "test city",
"country" : "Ireland",
"latitude" : null,
"name" : "test street",
"property_id" : "22"
}
]
}
正如您所看到的那样,我有三个数组,并且正在获取inspections_overdue_data
数组,我已成功获取但无法在tableview上显示
这是我的代码
代码
func OverdueList(){
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!, "access_token": A_Token!]
print(params)
Alamofire.request(inspectionsList, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let value):
let json = JSON(value)
print(json)
let data = json["inspections_overdue_data"]
print(data)
if data == []{
self.viewNodata.isHidden = false
}else{
data.array?.forEach({ (iunOverDue) in
let iOveList = OvedueModel(surveyor_id: iunOverDue["surveyor_id"].stringValue, country: iunOverDue["country"].stringValue, time: iunOverDue["time"].stringValue, address2: iunOverDue["address2"].stringValue, notes: iunOverDue["notes"].stringValue, house_num: iunOverDue["house_num"].stringValue, name: iunOverDue["name"].stringValue, address1: iunOverDue["address1"].stringValue, eircode: iunOverDue["eircode"].stringValue, date_inspected: iunOverDue["date_inspected"].stringValue, property_id: iunOverDue["property_id"].stringValue, county: iunOverDue["county"].stringValue, client_id: iunOverDue["client_id"].stringValue)
print(iOveList)
self.overDueData.append(iOveList)
})
self.tblOvedue.reloadData()
}
case.failure(let error):
print(error.localizedDescription)
}
})
}
我还通过情节提要添加了委托和数据源,但仍然无法在此处显示数据是我的tableview方法
func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return overDueData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! OverDueTableViewCell
let name = overDueData[indexPath.row].name
let address1 = overDueData[indexPath.row].address1
cell.lblTitle.text = "\(name) \(address1)"
return cell
}
答案 0 :(得分:2)
numberOfSections
返回0,如果您想查看任何内容,则必须至少返回1。
提示::如果只有1个部分,则可以忽略它,因为1是默认返回值。
答案 1 :(得分:1)
您应该删除以下 tableview的委托方法,或者至少应返回 1。
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}