UIRefreshControl不会消失

时间:2016-10-09 23:19:12

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

刷新的拉力并没有消失。屏幕上只显示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()
            })

        }
    }
}

2 个答案:

答案 0 :(得分:2)

首先,这一行是错误的:

tableView.addSubview(refreshControl!)...}

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

tableView.refreshControl = refreshControl

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

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

答案 1 :(得分:0)

试试这个。

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)