2016-11-02 8 views
1

UICollectionViewを使用してデータテーブルビューを作成しました。UICollectionViewのページ分割

これは、コードされています、私は最初の50件のレコードを

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呼び出しを行います。

しかし、そうすることで、私はindexPathが更新されますがどこで、なぜか、どのようにそれを更新するために理解することができませんでし取得されていない何とか考えてエラー

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

に取得しています。あなたがアドバイスしていただけますか?

+0

私が見てきましたそれは債券を上回る指数によるものです。あなたが配列に存在しないインデックスを取得しているように。これはメモリに関する問題ではありません。 –

答えて

1

もっと管理しやすい、collectionViewのwillDisplayCellメソッドを使用しないのはなぜですか。

私が代わりに

func scrollViewDidScroll(scrollView: UIScrollView) 

のあなたはUICollectionViewDelegateメソッドの下に使用することができると思う

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

: -

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

あなたは、コードの下に試すことができます。

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 
      } 
     }