scrollview 中的 Refreshcontrol 在 swift 中不起作用

Refreshcontrol in scrollview is not working in swift

提问人:Nirav Patel 提问时间:4/10/2019 更新时间:4/11/2019 访问量:1712

问:

我的观点中有一个可滚动的内容。我想在其中添加拉动以刷新。我尝试添加,但没有触发操作。

我尝试了很多方法并重新搜索,但没有一种对我有用。我将 iOS 10 作为最低部署目标。

尝试 1:

let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.addSubview(refreshControl)

尝试 2:

let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.refreshControl = refreshControl

MARK:- 刷新控制

@objc func pullToRefresh(_ refreshControl: UIRefreshControl) {
    // Update your conntent here
    self.setupData()
    //refreshControl.endRefreshing()
}
iOS Swift iPhone UIscrollView 下拉刷新

评论

0赞 Chowdhury Md Rajib Sarwar 2/20/2020
你能找到任何解决方案吗?我有同样的问题

答:

0赞 Mohammad Yunus 4/10/2019 #1

选择 TablViewController 转到检查器 查找刷新选择已启用 转到“文档大纲”,然后选择“刷新控件” 控制拖动或右键单击(按住)拖动到控制器文件并调用函数以刷新数据

它在 TableView 上不可用

0赞 Deviyani Swami 4/11/2019 #2
var refreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.delegate = self
    collectionView.dataSource = self

    // Add Refresh Control to collection View
    if #available(iOS 10.0, *) {
        collectionView.refreshControl = refreshControl
    } else {
        collectionView.addSubview(refreshControl)
    }

    refreshControl.tintColor = UIColor(red:0.25, green:0.72, blue:0.85, alpha:1.0)
    // Configure Refresh Control
    refreshControl.addTarget(self, action: #selector(fetchData(_:)), for: .valueChanged)
}


@objc private func fetchData(_ sender: Any) {

    DispatchQueue.main.async {
        //Fuction which you want to call
        self.refreshControl.endRefreshing()

    }
}