UIRefreshController Tableview 中的前 2 行处于非活动状态

UIRefreshController Top 2 Lines in Tableview inactive

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

我设置了我的应用程序,如果您点击 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


}

谢谢

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

refresh.endRefreshing()

我也纠结过这个问题,不过还好很简单。在ViewDidLoad中将UIRefreshControl发送到后面,大功告成!

Swift:

tableView.sendSubviewToBack(刷新)

Objective-C:

[tableView sendSubviewToBack:refresh];