如何在iOS swift中的表格视图按钮中进行拉动刷新[重复]

Posted

技术标签:

【中文标题】如何在iOS swift中的表格视图按钮中进行拉动刷新[重复]【英文标题】:How to make pull to refresh in tableview buttom in iOS swift [duplicate] 【发布时间】:2017-09-07 11:05:47 【问题描述】:

想要在 tableview 底部添加拉刷新。

首先要在tableview中显示数字100到90。

然后在tableview的底部添加pull to refresh。拉它想显示 80 到 70,然后拉刷新 70 到 60,然后拉刷新 50 到 40 ......等等最后的 1 到 10 意味着停止显示“无可用数据”。如何实现这一目标。提前帮我谢谢。

这是我的代码。

 @IBOutlet weak var dataTbl: UITableView!
    var numbers: [Any] = ["90","91","92","93","94","95","96","97","98","99","100"] // display first 100 to 90. 

在表格视图中加载。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return self.numbers.count
    



 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        let cell:UITableViewCell = self.dataTbl.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
        cell.textLabel?.text = self.numbers[indexPath.row] as? String

        return cell
    

在 google 中搜索了在 tableview 的按钮中添加刷新。我得到了这个代码。

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
 spinner.color = UIColor.darkGray
 spinner.hidesWhenStopped = true
 tableView.tableFooterView = spinner

显示微调器使用

spinner.startAnimating()

使用隐藏它

spinner.stopAnimating()

通过使用此代码如何进行拉刷新帮助我。

【问题讨论】:

但是达不到。 -- 表示什么... 你想用上面的代码做什么? 你在哪里称呼这个let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray) spinner.color = UIColor.darkGray 在tableview按钮上拉动刷新,先显示100到90再拉动刷新80到90,就像用10到1移动一样最终不显示数据 你得到我的问题了吗? 【参考方案1】:

使用以下委托来检测表格的结尾并使用表格页脚添加微调器逻辑。

func scrollViewDidScroll(_ scrollView: UIScrollView) 
            if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) 
                    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
     spinner.color = UIColor.darkGray
     spinner.hidesWhenStopped = true
     tableView.tableFooterView = spinner
            
        

将更多条目附加到数据源后,然后删除页脚视图

tableView.tableFooterView = nil

【讨论】:

如何刷新元素100到90、90到80、80到70 如果您从服务器获取它,那么您需要将偏移量发送到服务器以获取接下来的 10 条记录,当您从服务器获得响应时,然后将这些记录附加到以前的数据源【参考方案2】:

您将需要使用以下代码,

func scrollViewDidScroll(_ scrollView: UIScrollView) 
    if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) 
            //you reached the bottom of tableview, you can append the other 10 numbers to array and do reload
    

您不需要添加UIScrollViewDelegate 来实现这一点,我希望UITableViewDelegate 就足够了。由于UITableview 继承了UIScrollView 的属性 希望这会有所帮助

【讨论】:

以上是关于如何在iOS swift中的表格视图按钮中进行拉动刷新[重复]的主要内容,如果未能解决你的问题,请参考以下文章

从 iOS 13 中的呈现模式拉动以刷新表格视图不起作用

如何在 iOS 的 swift 中将浮动操作按钮放在 tableView 中?

如何更改 Swift 中表格视图单元格中的默认删除按钮?

表格视图部分 Swift3 的页脚中的按钮

如何在编辑表格视图单元格中的文本字段时重新加载所有表格视图单元格 - iOS Swift

如何在swift ios中的两个自定义表格视图单元格之间添加间距?