ios – 使用UITableView和数组的无限滚动

前端之家收集整理的这篇文章主要介绍了ios – 使用UITableView和数组的无限滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Swift的新手,我想用数组进行无限滚动.这是我的ClassViewController类

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上看到很多代码但是我不知道如何实现它们.

非常感谢.

解决方法

考虑使用 PagingTableView

class MainViewController: UIViewController {

  @IBOutlet weak var contentTable: PagingTableView!
  var legumes: [String] = ["Eggs","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
    }
  }

}

修改paginate函数以按您的意愿工作

猜你在找的iOS相关文章