Devanan Devanan - 3 months ago 9
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

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.

Comments