Before i start, i have went through each and every question relating to this issue. didnt help.
I want to refresh the json contents of my tableView which are fetched from a website.
The URL for each category comes from a different file that has All the categories listed in it.
The code that fetches the content is this
func animalSelected(animal: Animal) {
var request: NSURLRequest = NSURLRequest(URL: animal.url!)
self.refresh(animal.url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(animal.url!, completionHandler: {data, response, error -> Void in
// println("Task completed")
if(error != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let results: NSArray = jsonResult["posts"] as NSArray
self.didReceiveResults(jsonResult)
})
task.resume()
delegate?.collapseSidePanels?()
}
func didReceiveResults(results: NSDictionary) {
var resultsArr: NSArray = results["posts"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = resultsArr
self.tableView.reloadData()
})
}
My ViewDidLoad method and the refresh
method is as shown below
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to Refresh")
refresher!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
}
func refresh(categoryUrl : NSURL) {
var request: NSURLRequest = NSURLRequest(URL: categoryUrl)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(categoryUrl, completionHandler: {data, response, error -> Void in
if(error != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let results: NSArray = jsonResult["posts"] as NSArray
self.didReceiveResults(jsonResult)
})
task.resume()
self.refresher.endRefreshing()
}
I need to refresh the contents of the same URL that has been selected i.e refresh the same category.
I get this error : [UIRefreshControl absoluteURL]: unrecognized selector sent to instance 0x7fa0b3d73750
I think it is something related to absoluteURL
. Bt don't know what is it..
plsss plssssss plssss help
You're trying to get the absoluteURL
of a UIRefreshControl
that you think is actually an NSURL
.
To be exact, your sender in refresh(categoryUrl: NSURL)
is the UIRefreshControl
not NSURL
.