简体   繁体   English

UICollectionView 的分页

[英]Pagination for UICollectionView

I have created datatable view using UICollectionView.我使用 UICollectionView 创建了数据表视图。

This is the code:这是代码:

class PanelViewController: UIViewController{

var TableHeaderArray = NSMutableArray()
var TableDataArray = NSMutableArray()
var NumberOfRows = Int()   
var UlockDataArray = NSMutableArray()
let dataSource = DataSource()

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource.populateData()

    let noOfRows = dataSource.NumberOfRows
    self.NumberOfRows = noOfRows + 1

    self.UlockDataArray = dataSource.DUDataArray
    self.TableDataArray = dataSource.DTableDataArray   


    let layout: CustomCollectionViewLayout = CustomCollectionViewLayout()

    DataTableCollectionView = UICollectionView(frame: CGRect(x:0, y:0, width:self.TableVu.bounds.width, height:self.TableVu.bounds.height), collectionViewLayout: layout)
    DataTableCollectionView.dataSource = self
    DataTableCollectionView.delegate = self
    self.DataTableCollectionView .registerNib(UINib(nibName: "FixedCellIdentifier", bundle: nil), 
        forCellWithReuseIdentifier: FixedCellIdentifier)
    self.DataTableCollectionView .registerNib(UINib(nibName: "DynamoCellIdentifier", bundle: nil),
     forCellWithReuseIdentifier: DynamoCellIdentifier)

    self.TableVu.addSubview(DataTableCollectionView)        
    self.view.addSubview(TableVu)

}

} }

extension PanelViewController : UICollectionViewDataSource, UICollectionViewDelegate{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{        
        return self.NumberOfRows
    }

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {     
    return dataSource.TableHeaders.count        
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // My Code to Create Cells using data from TableHeaderArray, TableDataArray and UlockDataArray

}


func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
   let contentHeight = scrollView.contentSize.height
   var returnUmidData = NSMutableArray()
   var returnTableData = NSMutableArray()

   if offsetY > contentHeight - scrollView.frame.size.height {   

        self.dataSource.populateData()

        returnUData = self.dataSource.DUDataArray
        returnTableData = self.dataSource.DTableDataArray


        let seconds = 5.0
        let delay = seconds * Double(NSEC_PER_SEC)
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

        dispatch_after(dispatchTime, dispatch_get_main_queue(), {

             self.NumberOfRows += returnTableData.count

             let mergetWorkoutData = NSMutableArray(array: self.UlockDataArray)
             mergetWorkoutData.addObjectsFromArray(returnUmidData as [AnyObject])

             self.UlockDataArray = mergetWorkoutData

             let mergetWorkoutTabelData = NSMutableArray(array: self.TableDataArray)
             mergetWorkoutTabelData.addObjectsFromArray(returnTableData as [AnyObject])

             self.TableDataArray = mergetWorkoutTabelData 

            self.DataTableCollectionView!.reloadData()

        })
   }
}

} }

Initialy, I am displaying first 50 records.最初,我正在显示前 50 条记录。 I need to implement pagination on vertical scroll.我需要在垂直滚动上实现分页。 Each time user reach to end of scroll, I want to make an API call to get the next 50 records.每次用户到达滚动结束时,我都想调用 API 来获取接下来的 50 条记录。

But in doing so, I am getting an error但在这样做时,我收到了一个错误

"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]:
 index 51 beyond bounds [0 .. 50]'"

I think somehow indexPath is not getting updated but unable to understand where and why or how to update it.我认为 indexPath 没有得到更新,但无法理解在哪里、为什么或如何更新它。 Can you please advise?你能给些建议么?

Why don't you use collectionView's willDisplayCell method, which is more manageable. 为什么不使用collectionView的willDisplayCell方法,这种方法更易于管理。

func collectionView(_ collectionView: UICollectionView, 
             willDisplay cell: UICollectionViewCell, 
               forItemAt indexPath: IndexPath) {
    if indexPath.row + 1 == dataSource.count && dataSource.count < myPaginationUpperLimit {
        paginateMore()
    }
}

I think instead of 我想而不是

func scrollViewDidScroll(scrollView: UIScrollView)

you can use the below UICollectionViewDelegate method:- 你可以使用下面的UICollectionViewDelegate方法: -

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)`

You can try below code: 你可以尝试下面的代码:

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
        {
            if NumberOfRows >= indexPath.row
            {
                // call API
                // add data to array
                // reload collectionview
                NumberOfRows == TableDataArray.count
            }
        }

for easy way to use Pagination you need to use willDisplay cell mathod为了方便使用分页,您需要使用 willDisplay 单元格方法

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if currentPage < totalPage && indexPath.item == array.count - 1 {
            currentPage += 1
            // pass currenPage number in API
            // add data to array
            // reload collectionview
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView + AFNetworking +自动滚动/分页? - UICollectionView + AFNetworking + AutoScroll/Pagination? 在UICollectionView中滚动的分页 - Pagination on scroll in UICollectionView 如何在 UICollectionView 中快速添加分页? - How to add pagination in UICollectionView in swift? UICollectionView在分页期间不更新单元格 - UICollectionView don't update cell during pagination UICollectionView中的分页以及“下一个”和“上一个”单元格的预览 - Pagination in UICollectionView with the preview of the Next and Previous cell UICollectionView分页-如何获取最后一个可见的单元格 - UICollectionView Pagination - How to get the last visible cell 垂直滚动的 UICollectionview 中的分页,如何管理? - Pagination in a vertically scrolling UICollectionview, how to manage? 具有分页的UICollectionView。 将第二个单元格保持在屏幕中央 - UICollectionView with pagination. Keeping the second cell in center of the screen UICollectionView 的分页以在屏幕上显示一个完整的和两个部分的单元格 - Pagination of UICollectionView to show one full and two partial cells on the screen UICollectionView 用于小于 CollectionView 宽度的单元格的居中分页 - UICollectionView centered pagination for a cell smaller than CollectionView's width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM