Zizoo Zizoo - 3 months ago 17
Swift Question

Cleaning old UITableViewCells while navigating back and forth

I have two

UITableViewController
and I use navigation controller to go back from view#2 to view#1 .

In the first view I select some values from
UIPickerView
then by a button touch it will go to the second view and the the second view show some cells according to the
UIPicker
selection from view#1 , and here comes my problem when I go back to first view and change the selection of UiPicker and press the button to go to the second view I will find the old cells there for just a second until my view#2 get reload the new data inside this function

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}


I tried to remove the data from all arrays and reload again with empty data trying to remove the old cells like this :

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
arra1=[]
arra2=[]
self.tableView.reloadData()
}


and here my code that passing the data from view#1 to view#2:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

if (segue.identifier == "MySegue") {

let destinationVC:onCallEmployee = segue.destinationViewController as! onCallEmployee


if self.selectedDeptIndex != 0 {
destinationVC.previewsViewData.append(String(self.selectedDeptIndex))
}
if self.teamsIdByName[selectedTeamName] != nil{
destinationVC.previewsViewData.append(self.teamsIdByName[selectedTeamName]!)
}
if self.servicesIdByName[selectedServiceName] != nil {
destinationVC.previewsViewData.append(self.servicesIdByName[selectedServiceName]!)
}

}
}


Is there any way to clean the view#2 before going back to view#1?

Answer

Use this:

dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            })

instead of this:

self.tableView.reloadData()
Comments