UIRefreshController Tableview中的前2行无效

时间:2016-01-05 08:44:49

标签: swift tableview uirefreshcontrol

刚开始我会说我是新手,但无论如何。

我设置了自己的应用,如果您点按tableview的一行,它会将您带到您所选位置的地图。当我为我的UIRefreshControl注释掉我的代码时,一切都按照我想要的方式运行,但当我把它还给我时,我无法点击前两个 tableview

请在下面找到我发表评论的代码。

refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to See     Closest Locations")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)

整个守则

import UIKit
import Parse


var manager: CLLocationManager!
var latitude1: CLLocationDegrees = 0
var longitude1: CLLocationDegrees = 0
var distances = [CLLocationDistance]()
var places = [Dictionary<String,String>()]

var activeplace = -1

var refresher: UIRefreshControl!

class TableViewController: UITableViewController, CLLocationManagerDelegate {

func refresh () {

    var newPlaces = [Dictionary<String,String>()]
    var newDistances = [CLLocationDistance]()
    if newPlaces.count == 1 {
        newPlaces.removeAtIndex(0)}
    let query = PFQuery(className: "Lights")
    query.selectKeys(["name","Geo"])

    query.whereKey("Geo", nearGeoPoint:PFGeoPoint(latitude: latitude1, longitude: longitude1))
    query.limit = 200
    query.findObjectsInBackgroundWithBlock({ (nameU, error) -> Void in
        if error != nil {
            print(error)
        } else {
            if let objects = nameU {
                for object in objects {

                    let name = object["name"] as! String
                    let geo = object["Geo"] as! PFGeoPoint

                    let lat = geo.latitude
                    let lon = geo.longitude

                    let lightLocation = CLLocationCoordinate2DMake(lat, lon)
                    let LightCLLocation = CLLocation(latitude: lightLocation.latitude, longitude: lightLocation.longitude)
                    let userLocation1 = CLLocation(latitude: latitude1, longitude: longitude1)

                    let distance = userLocation1.distanceFromLocation(LightCLLocation)

                    let distance1 = round(distance/100 * 0.621371)

                    newDistances.append(distance1/10)

                    newPlaces.append(["name":name,"lat":"\(lat)","lon":"\(lon)"])

                }

            }
            places.removeAll()
            distances.removeAll()
            places = newPlaces
            distances = newDistances
        }
        self.tableView.reloadData()
        refresher.endRefreshing()

    })



}


override func viewDidLoad() {
    super.viewDidLoad()

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to See Closest Locations")
    refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refresher)

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    if #available(iOSApplicationExtension 8.0, *) {
        manager.requestWhenInUseAuthorization()
    } else {
        // Fallback on earlier versions
    }
    manager.startUpdatingLocation()


    if places.count == 1 {

        places.removeAtIndex(0)
    }

    let query = PFQuery(className: "Lights")
    query.selectKeys(["name","Geo"])
    query.whereKey("Geo", nearGeoPoint:PFGeoPoint(latitude: latitude1, longitude: longitude1))
    query.limit = 200


    query.findObjectsInBackgroundWithBlock({ (nameU, error) -> Void in
        if error != nil {
            print(error)
        } else {

            // needs [PFObject] added
            if let objects = nameU {
                for object in objects {

                    let name = object["name"] as! String
                    let geo = object["Geo"] as! PFGeoPoint

                    let lat = geo.latitude
                    let lon = geo.longitude

                    let lightLocation = CLLocationCoordinate2DMake(lat, lon)
                    let LightCLLocation = CLLocation(latitude: lightLocation.latitude, longitude: lightLocation.longitude)
                    let userLocation1 = CLLocation(latitude: latitude1, longitude: longitude1)

                    let distance = userLocation1.distanceFromLocation(LightCLLocation)

                    let distance1 = round(distance/100 * 0.621371)

                    distances.append(distance1/10)



                    places.append(["name":name,"lat":"\(lat)","lon":"\(lon)"])

                    self.tableView.reloadData()

                }

            }
        }

    })




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
        return places.count


}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        cell.textLabel?.text = places[indexPath.row]["name"]! + "  -  " + String(distances[indexPath.row]) + " Miles Away"

    return cell
}


override func viewWillAppear(animated: Bool) {

    tableView.reloadData()

}

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

    activeplace = indexPath.row

    return indexPath
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "newPlace" {

        activeplace = -1
    }


}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation:CLLocation = locations[0]
    latitude1 = userLocation.coordinate.latitude
    longitude1 = userLocation.coordinate.longitude


}

由于

2 个答案:

答案 0 :(得分:2)

确保您的目标操作正确无误。完成刷新后,调用此方法停止并隐藏UIRefreshControl。

refresh.endRefreshing()

答案 1 :(得分:0)

我也在努力解决这个问题,但事实证明这很简单。在ViewDidLoad中将UIRefreshControl发送到后面,你就完成了!

夫特:

tableView.sendSubviewToBack(刷新)

目标-C:

[tableView sendSubviewToBack:refresh];