简体   繁体   中英

reload tableview view like refresh

I am populating tableview with json data and its work successfully but now I want to update tableview on refresh button click in refresh button i call again existing func which I create to call api let me show you my code

Code

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)
        }

    })
}

I am calling this func for populating table view and I am doing like below

@IBAction func btnRefreshTapped(_ sender: UIButton) {
      OverdueList()
    }

the issue was that after re calling this function data again and again repeated so how can I solve this problem?

Please try this code :

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)
        }

    })
}

It may helps you thank you.

Clear here

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()
}

You can also declare the array as Set to remove similar objects

It's simple...just because on refresh you are calling 'OverdueList' but failed to clear data from an array of overDueData. That's the reason you are getting data repeated.

For this, you just need to do this as simple.

@IBAction func btnRefreshTapped(_ sender: UIButton) {    
     self.overDueData.removeAll()
     self.OverdueList()    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM