UIRefreshController Tableview 中的前 2 行不活动

Posted

技术标签:

【中文标题】UIRefreshController Tableview 中的前 2 行不活动【英文标题】:UIRefreshController Top 2 Lines in Tableview inactive 【发布时间】:2016-01-05 08:44:49 【问题描述】:

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

我设置了我的应用程序,如果您点击tableview 的一行,它会将您带到您选择的位置的地图。当我为我的UIRefreshControl 注释掉我的代码时,一切都按照我想要的方式工作,但是当我把它带回来时,我无法点击tableview 的前两行 rows .

请在下面找到我注释掉的代码。

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



谢谢

【问题讨论】:

当您取消注释除最后一行之外的所有内容时会发生什么? 那么刷新器不起作用,但是上面两行起作用 【参考方案1】:

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

refresh.endRefreshing()

【讨论】:

我刚刚发现在我刷新一切正常之前,我刷新后前两行将无法正常工作。我的刷新函数中有 refresh.endRefreshing() 代码。 你的代码就好了。尝试卸载该应用程序并重新构建它。也许你可以先清理应用程序,进入栏菜单,产品 -> 清理或 Cmd+Shift+K。【参考方案2】:

我也遇到过这个问题,但结果很简单,幸运的是。在 ViewDidLoad 中将 UIRefreshControl 发送到后面,就大功告成了!

斯威夫特:

tableView.sendSubviewToBack(刷新)

目标-C:

[tableView sendSubviewToBack:refresh];

【讨论】:

以上是关于UIRefreshController Tableview 中的前 2 行不活动的主要内容,如果未能解决你的问题,请参考以下文章

UIRefreshController Tableview 中的前 2 行不活动

没有 tableView 的 UIRefreshControl

在 UIViewController 中使用 UIRefreshControl 和 UITableView

ios中uiscrollview的底部刷新控件

Swift Firebase UIRefreshControl 创建重复的帖子

ios7将刷新控件添加到表视图作为子视图后,它增加了额外的空间