Rutuparna Panda
Rutuparna Panda

Reputation: 39

How to reload tableview cell data using swift

I have a tableview and two dropdown. When I am changing dropdown value cell data are repeating two times. I want to change my tableview cell data according to the dropdown changed value. When I will change the dropdown value the first data should be deleted and again it will be loaded according to the value changed in dropdown.

I had tried tableview.reloadData() in viewDidLoad() and also in my dropdown changed action method but it is not working.

select_activity.didSelect{(selectedText , index ,id) in
            self.select_subactivity.isUserInteractionEnabled = true
            self.select_subactivity.text = ""
            self.getListOfSubactivity(activity_id: id)
            if(id == 2) {
                self.select_subactivity.didSelect{(selectedText, index, id) in
                    //self.Summary_tableView.isHidden = false
                    self.Summary_tableView.reloadData()
                    let user_id = UserDefaults.standard.string(forKey: "user_id")!
                    let url = self.appDelegate.BaseUrl + "woodland/summary"
                    self.SummaryApi(url: url, userid: user_id, cph_id: cph_id!, subactivity_name: id)
                }
            }
        }

Any help will be highly appreciated!

Upvotes: 0

Views: 402

Answers (2)

Devanshi Modha
Devanshi Modha

Reputation: 359

You should remove all the elements from the array upon selection the value from the dropdown and then reload the table

Following Steps should be followed upon selecting a value from the dropDown:

  1. Select the Value
  2. Clear the array (array.removeAll()) to avoid duplication
  3. Call the API
  4. Reload the UITableView

Upvotes: 1

Shovo
Shovo

Reputation: 129

viewDidLoad() is called once after the view controller’s view has been loaded into memory. That's why your tableview is not reloading. you should try to call tableview.reloadData() where you are changing the dropdown value.

Upvotes: 0

Related Questions