你好,我成功地用json数据填充了tableview及其工作,但是现在我想更新Referh按钮上的tableview单击刷新按钮,我再次调用现有的func
,我创建它来调用api,让我向你展示我的代码
代码
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)
SVProgressHUD.show()
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, address3: iunOverDue["address3"].stringValue, notes: iunOverDue["notes"].stringValue, house_num: iunOverDue["house_num"].stringValue, name: iunOverDue["name"].stringValue, address1: iunOverDue["address1"].stringValue, latitude: iunOverDue["latitude"].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, longitude: iunOverDue["longitude"].stringValue)
self.searchResult = self.overDueData
self.overDueData.append(iOveList)
})
self.tblOvedue.reloadData()
}
SVProgressHUD.dismiss()
case.failure(let error):
print(error.localizedDescription)
}
})
}
我正在调用此func来填充表格视图,并且我的操作如下所示
@IBAction func btnRefreshTapped(_ sender: UIButton) {
OverdueList()
}
问题在于,一次又一次地重复调用此函数数据后,我该如何解决这个问题
请帮助我解决这个问题
答案 0 :(得分:3)
请尝试以下代码:
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)
SVProgressHUD.show()
Alamofire.request(inspectionsList, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let value):
self.overDueData.removeAll()
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, address3: iunOverDue["address3"].stringValue, notes: iunOverDue["notes"].stringValue, house_num: iunOverDue["house_num"].stringValue, name: iunOverDue["name"].stringValue, address1: iunOverDue["address1"].stringValue, latitude: iunOverDue["latitude"].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, longitude: iunOverDue["longitude"].stringValue)
self.searchResult = self.overDueData
self.overDueData.append(iOveList)
})
self.tblOvedue.reloadData()
}
SVProgressHUD.dismiss()
case.failure(let error):
print(error.localizedDescription)
}
})
}
它可以帮助您谢谢您。
答案 1 :(得分:1)
在这里清除
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{
self.overDueData.removeAll()
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, address3: iunOverDue["address3"].stringValue, notes: iunOverDue["notes"].stringValue, house_num: iunOverDue["house_num"].stringValue, name: iunOverDue["name"].stringValue, address1: iunOverDue["address1"].stringValue, latitude: iunOverDue["latitude"].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, longitude: iunOverDue["longitude"].stringValue)
self.searchResult = self.overDueData
self.overDueData.append(iOveList)
})
self.tblOvedue.reloadData()
}
您还可以将数组声明为Set以删除相似的对象
答案 2 :(得分:0)
这很简单...只是因为在刷新时您正在调用“ OverdueList”,但无法从overDueData数组中清除数据。这就是重复获取数据的原因。
为此,您只需要简单地完成此操作即可。
@IBAction func btnRefreshTapped(_ sender: UIButton) {
self.overDueData.removeAll()
self.OverdueList()
}