Devanan Devanan - 1 year ago 77
Swift Question

Swift: Using Refresh Button is loading data with existing data in collectionview

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 data is displaying in the collection view below the existing data. Can someone please help me on how to erase the existing data and load only the new data when refresh button is tapped.

Answer Source

func Dataparsed() {

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

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

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

        catch {
            print("json error: \(error)")

Hope It Helps.Thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download