UIRefreshControl 不消失

UIRefreshControl not Dissapearing

拉动刷新并没有消失。屏幕上只有 3 行可见,使用 print(indexPath.row) 后我可以看到它再次重新加载第 0、1、2 行,但刷新控件没有消失。

override func viewDidLoad() {
    super.viewDidLoad() ... 
        refreshControl = UIRefreshControl()
        refreshControl!.addTarget(self, action: #selector(self.loadData), forControlEvents: UIControlEvents.ValueChanged)
        refreshControl!.backgroundColor = UIColor.clearColor()
        refreshControl!.tintColor = UIColor.clearColor()
        tableView.addSubview(refreshControl!)...}

这是加载数据函数:

func loadData() {
    venues = [CKRecord]()

    let location = locationManager.location

    let radius = CGFloat(1000)

    let sort = CKLocationSortDescriptor(key: "Location", relativeLocation: location!)

    let predicate = NSPredicate(format: "distanceToLocation:fromLocation:(%K,%@) < %f", "Location", location!, radius)

    let publicData = CKContainer.defaultContainer().publicCloudDatabase

    let query = CKQuery(recordType: "Venues", predicate: predicate )

    query.sortDescriptors = [sort]


    publicData.performQuery(query, inZoneWithID: nil) { (results:[CKRecord]?, error:NSError?) in
        if let venues = results {
            self.venues = venues
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            })

        }
    }
}

首先,这一行是错误的:

tableView.addSubview(refreshControl!)...}

您不需要将刷新控件添加到 iOS 10 中的 table 视图作为子视图。它有一个 refreshControl 属性set:

tableView.refreshControl = refreshControl

其次,您完全正确,刷新控件不会自行消失。重新加载数据后,由 向刷新控件发送 endRefreshing() 消息。

        dispatch_async(dispatch_get_main_queue(), {
            self.tableView.reloadData()
            self.tableView.refreshControl.endRefreshing()
        })

试试这个。

var refreshcontroller : UIRefreshControl = UIRefreshControl()

创建函数。

func refresh(sender:AnyObject) {
        refreshcontroller.endRefreshing()
}

将此行放入您的 viewDidLoad()

你也可以在UIRefreshControl中添加图片

refreshcontroller = UIRefreshControl()
let rcImageView = UIImageView(image: UIImage(named: "refreshControl.png")!)
refreshcontroller.attributedTitle = NSAttributedString(string:"")
refreshcontroller.addTarget(self, action: #selector(self.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
refreshcontroller.insertSubview(rcImageView, atIndex: 0)
self.Songlisttable.addSubview(refreshcontroller)