代码之家  ›  专栏  ›  技术社区  ›  user3700449

向下滚动表视图时添加分页

  •  0
  • user3700449  · 技术社区  · 3 年前

    我想对数据进行分页。滚动表视图时添加分页的最佳方式是什么?我尽量不同时显示所有数据。例如,每当用户点击表视图的底部时,接下来的10个表视图单元格就会加载到(10、20、30等等)中。我使用的是Swift和Firestore。这是我的密码。谢谢

         import UIKit
         import FirebaseCore
         import FirebaseFirestore
         import FirebaseAuth
    
         class MainTableViewController: UIViewController {
    
         var itemArray:[Item] = []
         var item:Item!
    
    
         override func viewDidLoad() {
        super.viewDidLoad()
        
        
       loadListingItems()
      
        
        }
    
    
    func ListingdownloadITemsFromFirebase(completion: @escaping (_ 
     itemArray: [Item]) -> Void) {
        
        var itemArray: [Item] = []
        
      
       FirebaseReference(.Items).whereField(KISCAR, isEqualTo: true) 
        .order(by: kDATEPOSTED , descending: true).limit(to: 10
        ).getDocuments { (snapshot, error) in
            
            guard let snapshot = snapshot else {
                completion(itemArray)
                return
            }
            
            if !snapshot.isEmpty {
                
                for itemDict in snapshot.documents {
                    
                    itemArray.append(Item(_dictionary: 
               itemDict.data() as NSDictionary))
                }
            }
            
            completion(itemArray)
        }
    }
    
        private func loadListingItems() {
        
        ListingdownloadITemsFromFirebase { (allITems) in
            print("we have ",allITems.count)
            self.itemArray = allITems
            self.tableView1.reloadData()
        }
    }
    
         func tableView(_ tableView: UITableView, 
          numberOfRowsInSection section: Int) -> Int {
        
            return itemArray.count
            
        }
        
    
       func tableView(_ tableView: UITableView, cellForRowAt 
       indexPath: IndexPath) -> UITableViewCell {
    
       cell.generateCell(itemArray[indexPath.row], indexPath: 
       indexPath)
    
    }
    
    }
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Prabir    3 年前

    Firestore提供了带有查询游标的分页,这些游标可用于以您想要的方式实现分页。 This document 详细解释了如何在Firestore中使用分页。

    按照您希望获取数据的方式,最好实现一个逻辑,以便在每次用户点击表视图底部时重新运行查询 limit (10,20,30等等),这将逐渐获取增加数量的记录。我会建议你通过 this video 了解更多关于它的信息,并找出哪种类型的分页适合您。

    我建议你参考类似的 StackOverFlow thread 这将对你有所帮助。