i've add Pull To Refresh on UITableView on my Swift project successful, on another ViewController i'm not able to show it. On the others view the code is the same without LocationManager functions. I don't know where is my error!
Below my code:
import UIKit
import CoreLocation
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
var locationCoordinates: CLLocationCoordinate2D!
@IBOutlet weak var bannerView: GADBannerView!
var dati = NSMutableArray()
var datiComplete = NSDictionary()
@IBOutlet weak var tableView: UITableView!
var arrayOfData: [MyData] = [MyData]()
var url:NSURL!
var refreshControl = UIRefreshControl()
var dateFormatter = NSDateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest //Battery drain!
self.locationManager.distanceFilter = 1
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
searchUser()
self.dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
self.dateFormatter.timeStyle = NSDateFormatterStyle.LongStyle
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.insertSubview(refreshControl, atIndex: 0)
self.handleRefresh()
}
func refresh(sender:AnyObject)
{
println("Refresh work!")
self.handleRefresh()
}
func handleRefresh() {
if locationManager.location != nil {
url = NSURL(string: "http://www.myURL.com/data.php?lat=\(locationManager.location.coordinate.latitude)&lon=\(locationManager.location.coordinate.longitude)&max=15&when=now")!
} else {
url = NSURL(string: "http://www.myURL.com/data.php?lat=41&lon=11&max=10&when=now")!
}
//println("Call URL!!")
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Accept")
var reponseError: NSError?
var response: NSURLResponse?
//var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
if (error != nil) {
return
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
var error: NSError?
self.dati = (NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &error) as! NSDictionary)["spots"] as! NSMutableArray
if (error != nil){
return
}
// **** Json Parsing *****
dispatch_async(dispatch_get_main_queue()){
self.tableView.reloadData()
self.tableView.delegate = self
self.tableView.dataSource = self
}
}
})
let now = NSDate()
let updateString = "Last Updated at " + self.dateFormatter.stringFromDate(now)
self.refreshControl.attributedTitle = NSAttributedString(string: updateString)
if self.refreshControl.refreshing
{
self.refreshControl.endRefreshing()
}
self.tableView?.reloadData()
refreshControl.endRefreshing()
}
func searchUser(){
println("Start Search User")
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
println("Start Location Manager Func")
self.locationCoordinates = manager.location.coordinate
self.locationManager.stopUpdatingLocation()
println("**************** locations = \(self.locationCoordinates.latitude) \(self.locationCoordinates.longitude)")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setNavigationBarItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: ViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ViewCell
cell.backgroundColor = UIColor.whiteColor()
let usr = arrayOfData[indexPath.row]
cell.setCell(<Cell-data>)
return cell
}
var selectedSpot:String? = nil
var selectedSpotIndex:Int? = nil
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "GoSpotDetails" {
var tabBarC : TabBarController = segue.destinationViewController as! TabBarController
var caseIndex = tableView!.indexPathForSelectedRow()!.row
var selectedCase = self.arrayOfSpotsTemp[caseIndex]
tabBarC.DataDetail = selectedCase
}
}
}
Thanks a lot.