【问题标题】:UIRefreshControl not DissapearingUIRefreshControl 没有消失
【发布时间】:2026-02-08 09:05:01
【问题描述】:

拉动刷新并没有消失。屏幕上只有 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!)...}

这里是 loadData 函数:

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()
            })

        }
    }
}

【问题讨论】:

  • 试试我的代码,你的问题会很容易解决的。

标签: ios swift uitableview pull-to-refresh uirefreshcontrol


【解决方案1】:

首先,这一行是错误的:

tableView.addSubview(refreshControl!)...}

您不需要在 iOS 10 中将刷新控件作为子视图添加到表视图中。它有一个您设置refreshControl 属性

tableView.refreshControl = refreshControl

其次,刷新控件不会自行消失是绝对正确的。当您重新加载数据后,可以向刷新控件发送endRefreshing() 消息。

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

【讨论】:

  • 另外,我强烈建议升级到 Xcode 8。Swift 3 是事实。你应该使用它。
【解决方案2】:

试试这个。

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)

【讨论】: