UICollectionView的分页

时间:2016-11-02 07:57:58

标签: ios swift

我使用UICollectionView创建了数据表视图。

这是代码:

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()

        })
   }
}

}

最初,我正在显示前50条记录。我需要在垂直滚动上实现分页。 每次用户到达滚动结束时,我都想进行API调用以获取接下来的50条记录。

但是这样做,我收到了错误

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

我认为indexPath没有得到更新,但无法理解更新它的位置和原因。你能告诉我吗?

3 个答案:

答案 0 :(得分:3)

为什么不使用collectionView的willDisplayCell方法,这种方法更易于管理。

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

答案 1 :(得分:0)

我认为不是

func scrollViewDidScroll(scrollView: UIScrollView)

您可以使用以下UICollectionViewDelegate方法: -

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

答案 2 :(得分:0)

您可以尝试以下代码:

new HttpClient().PostAsync("http://...", new JsonContent(new { x = 1, y = 2 }));