简体   繁体   English

表格视图以编程方式滚动到顶部后,UIRefreshControl 不会消失

[英]UIRefreshControl is not disappearing after the tableview scrolls to top programmatically

When i want to the update data source of the tableview, firstly i want to scroll to top (to header view) then show the UIRefreshControl view, after data source updated then i want to hide the UIRefreshControll.当我想更新 tableview 的数据源时,首先我想滚动到顶部(到标题视图)然后显示 UIRefreshControl 视图,在数据源更新后我想隐藏 UIRefreshControll。

To do that i tried following line:为此,我尝试了以下行:

self.kisilerTable.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: false)
self.kisilerTable.setContentOffset(CGPoint(x: 0, y: self.kisilerTable.contentOffset.y - (self.refreshControl.frame.size.height)), animated: true)
self.kisilerTable.refreshControl?.beginRefreshing()

Above code is scrolling to top then it shows the UIRefreshControl.上面的代码滚动到顶部然后显示 UIRefreshControl。 And after my data updated i tried to hide it with following code:在我的数据更新后,我尝试使用以下代码隐藏它:

self.kisilerTable.reloadData()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
    self.kisilerTable.refreshControl?.endRefreshing()
}

But the problem is, above code is hiding the indicator animation but it leaves a space there.但问题是,上面的代码隐藏了指标动画,但在那里留下了空间。 As you can see in the screenshot:正如您在屏幕截图中看到的:

在此处输入图片说明 在此处输入图片说明

If i try to hide with following code:如果我尝试使用以下代码隐藏:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
    self.kisilerTable.reloadData()
    self.kisilerTable.refreshControl?.endRefreshing()
}

It is hiding but, i am losing the hide animation.它正在隐藏,但是,我正在丢失隐藏动画。 I don't want to lose hide animation.我不想丢失隐藏动画。

How can i resolve this problem?我该如何解决这个问题?

I think it happens, because in the beginning you use setContentOffset to leave a space, but that space is not removed.我认为它发生了,因为一开始你使用setContentOffset来留下一个空间,但那个空间没有被删除。

I will suggest another implementation, which works very well:我会建议另一种实现,效果很好:

First you declare the refreshControll before the method viewDidLoad() :首先在方法viewDidLoad()之前声明refreshControll

 let refreshControl = UIRefreshControl()

Now do you need create a method for stop the animation when needed:现在您是否需要创建一个在需要时停止动画的方法:

func stopAnimatingRefreshControl() {
    if let refreshControl = 
       tableView.subviews.first(where: { $0 is UIRefreshControl})as? UIRefreshControl,
       refreshControl.isRefreshing {

        refreshControl.endRefreshing()
    }
}

Now you create an @objc function that be called when scrolls the table to update, normally in this moment you call methods that make your server request to update data.现在您创建了一个 @objc 函数,当滚动表进行更新时会调用该函数,通常此时您会调用使服务器请求更新数据的方法。

When this data is updated, you call the function stopAnimatingRefreshControl():当此数据更新时,您调用函数 stopAnimatingRefreshControl():

@objc func refreshMyData() {
    // call methods for get data from server
    getDataFromServer()
}

func getDataFromServer() {
   // request completed and data is updated, now i need stop the loading.

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }

    stopAnimatingRefreshControl()
}

Finally, after creating the necessary methods, you need to link our @objc function to refreshControll and our tableView , do it in method viewDidLoad() :最后,在创建必要的方法后,您需要将我们的@objc 函数链接到refreshControll和我们的tableView ,在方法viewDidLoad() 中执行此操作

refreshControl.addTarget(self, action: #selector(refreshMyData), for: .valueChanged)
tableView.addSubview(refreshControl)

Now you're done, I hope I helped.现在你完成了,我希望我有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM