【问题标题】:Infinite scroll with UITableView and an Array使用 UITableView 和数组进行无限滚动
【发布时间】:2017-08-14 19:11:57
【问题描述】:

我是 Swift 的新手,我想用一个数组制作一个无限滚动。这是我的类 TableViewController

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  var legumes: [String] = ["Eggs", "Milk", "Chocolat", "Web", "Miel", "Pop", "Eco", "Moutarde", "Mayo", "Thea", "Pomelade", "Gear", "Etc" , "Nop", "Dews", "Tout", "Fun", "Xen" , "Yoga" ]
  override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    func numberOfSections(in tableView: UITableView) -> Int {
      return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return self.legumes.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageTableViewCell
      return cell
    }
}

我想显示我的数组的前十个项目,当我在 TableViewController 的底部时,它会加载接下来的十个项目,等等。我不知道该怎么做,我看到很多代码在 GitHub 上,但我不知道如何实现它们。

非常感谢。

【问题讨论】:

  • 您正在尝试做的事情称为延迟加载。搜索一下 swift tableview 延迟加载,你会得到更多的例子。

标签: ios arrays swift uitableview infinite-scroll


【解决方案1】:

您所描述的内容称为pagination。你应该这样做:

/* Number of page you're loading contents from */
var pageIndex: Int = 1

override func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height

    if offsetY > contentHeight - scrollView.frame.size.height {
        /* increment page index to load new data set from */
        pageIndex += 1

        /* call API to load data from next page or just add dummy data to your datasource */
        /* Needs to be implemented */
        loadNewItemsFrom(pageIndex)

        /* reload tableview with new data */
        tableView.reloadData()
    }
}

【讨论】:

  • 函数 loadNewItems 是什么?我不明白它是如何工作的。感谢您的反馈
  • @Yapoto 你应该实现它。它从服务器加载新数据或将虚拟数据添加到数据源(如array += array)。这是一种算法,实现可能会有所不同。
  • 使用reloadData 不适用于延迟加载,因为它会将滚动重置到顶部。
【解决方案2】:

考虑使用PagingTableView

class MainViewController: UIViewController {

  @IBOutlet weak var contentTable: PagingTableView!
  var legumes: [String] = ["Eggs", "Milk", "Chocolat", "Web", "Miel", "Pop", "Eco", "Moutarde", "Mayo", "Thea", "Pomelade", "Gear", "Etc" , "Nop", "Dews", "Tout", "Fun", "Xen" , "Yoga" ]

  override func viewDidLoad() {
    super.viewDidLoad()
    contentTable.dataSource = self
    contentTable.pagingDelegate = self
  }

}

extension MainViewController: UITableViewDataSource {

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

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageTableViewCell
    guard legumes.indices.contains(indexPath.row) else { return cell }
    cell.content = legumes[indexPath.row]
    return cell
  }

}

extension MainViewController: PagingTableViewDelegate {

  func paginate(_ tableView: PagingTableView, to page: Int) {
    contentTable.isLoading = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      self.legumes.append(contentsOf: legumes)
      self.contentTable.isLoading = false
    }
  }

}

修改分页功能以按您的意愿工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 2015-06-03
    • 2018-10-27
    • 2016-04-02
    • 2021-07-20
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多