swift ios 向 uitableview 添加无限滚动分页 [英] swift ios add infinite scroll pagination to uitableview

查看:27
本文介绍了swift ios 向 uitableview 添加无限滚动分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 tableview 是否有任何内置函数来添加无限滚动/分页.

现在我的 VC 看起来像这样:

var 数据:JSON!= []覆盖 func viewDidLoad() {super.viewDidLoad()//初始化单元格起始高度self.tableView.estimatedRowHeight = 122self.tableView.rowHeight = UITableViewAutomaticDimensionself.tableView.delegate = selfself.tableView.dataSource = selfsavedLoader.startAnimation()//加载第一页加载保存(1)}func tableView(tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回数据.count}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("aCell") as!保存的TableViewCell让信息 = 数据[indexPath.row]cell.configureWithData(info)返回单元格}func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {performSegueWithIdentifier("WebSegue", sender: indexPath)tableView.deselectRowAtIndexPath(indexPath, 动画: false)}

我使用 loadSaved(1) 通过为函数提供我想要加载的当前页面来获取我的数据.该函数使用 alomofire 发出 API 请求,然后填充 var 数据:JSON!= []应显示的数据

所以我想做的是当我滚动到 tableview 的底部时 loadSaved(2) 应该被称为将更多数据加载到 tableview 中

解决方案

UITableViewDelegate 有一个 table View(_: will Display: for Row At: ) 实例方法告诉代理表格视图即将为特定行绘制单元格.">

在你的情况下,我会像这样使用它:

override open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {if indexPath.row == data.count-1 {//你可能决定早于 -1 加载我猜...//在此处加载更多数据}}

根据您的代码,您可能需要对此进行一些检查,以确保在加载所有数据后不会陷入无限循环......

I wonder if tableview has any built-in function to add infinite scroll/pagination.

Right now my VC looks like this:

var data: JSON! = []

override func viewDidLoad() {
    super.viewDidLoad()

    //Init start height of cell
    self.tableView.estimatedRowHeight = 122
    self.tableView.rowHeight = UITableViewAutomaticDimension

    self.tableView.delegate = self
    self.tableView.dataSource = self

    savedLoader.startAnimation()
    //Load first page
    loadSaved(1)

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("aCell") as! SavedTableViewCell

    let info = data[indexPath.row]
    cell.configureWithData(info)

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("WebSegue", sender: indexPath)

    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}

I fetch my data using loadSaved(1) by giving the function the current page I want to load. The function makes a API request using alomofire then populate the var data: JSON! = [] with the data that should be displayed

So what I want to do is when I scroll to the bottom of the tableview loadSaved(2) should be called loading more data into the tableview

解决方案

The UITableViewDelegate has a table​View(_:​will​Display:​for​Row​At:​) instance method which "tells the delegate the table view is about to draw a cell for a particular row."

In your case I would use it something like this:

override open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == data.count-1 { //you might decide to load sooner than -1 I guess...
      //load more into data here
    }
}

Depending on your code, you may need some checks around this to ensure you don't end up in an infinite loop if you've loaded all your data...

这篇关于swift ios 向 uitableview 添加无限滚动分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆