user6592532
user6592532

Reputation:

Swift: Refresh Button functionality is loading the new data including the existing data

I am loading data on collection view from a set of JSON Data. I have added a refresh button on the navigation bar. I have used the same function as used in Viewdidload to the refresh button. When I tap on the refresh button, The new data is displayed after the existing data in the collection view rather than displaying just the new data. Can someone please help me on how to erase the existing data and display only the new data when refresh button is tapped.

Upvotes: 0

Views: 776

Answers (2)

Avinash Mishra
Avinash Mishra

Reputation: 797

func Dataparsed() {

    let quizurl =  NSURL(string:"Your URL")
    let task = NSURLSession.sharedSession().dataTaskWithURL((quizurl)!, completionHandler: { (data, response, error) -> Void in
        do{
            let   string = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary



            let swapLibs = string as? NSDictionary
            let QuizData  = swapLibs!.valueForKey("Your Key") as! NSArray
            self.YourMutableArray.removeAllObjects()

            for dict in QuizData {
                self.playQuizArray.addObject(playInfo.PlayQuizInfo(dict as? NSDictionary))
            }
            self.colletionView.reloadData()

                print(self.playQuizArray)
        }
        catch {
            print("json error: \(error)")
        }
    })
    task.resume()
}

Hope It Helps.Thanks.

Upvotes: 1

Fahad Rehman
Fahad Rehman

Reputation: 1199

when you recieve the json data on button click try to clear the list first

var someArray = [string]()
someArray.RemoveAll();

then add the new data loaded from json after that reload the collection

collectionview.ReloadData()

Upvotes: 1

Related Questions