Search code examples
swiftasyncdisplaykit

Is there a way to insert new items while loading more on ASCollectionNode Horizontal Scrolling


I'm using ASCollectionNode horizontal scrolling and I'm trying to archive a scroll left to load more and insert items.but when I reach to the last item on the left the items are been inserted on the left instead of Right.

I implemented the insert like this..

 func addRowsIntoPhotoCollection(newPhotoCount newPhotos: Int) {
 let indexRange = (photoFeedModel.numberOfItems - newPhotos..<photoFeedModel.numberOfItems)
    let indexPaths = indexRange.map {  IndexPath(row: $0, section: 0) }
    collectionView.performBatchUpdates({
        collectionView.insertItems(at: indexPaths)
    }) { (done) in
        if self.shouldScroll == true {
              self.scrollToBeginning()
        }
    }
}

Solution

  • For add cells on the end of the list you have to create array of IndexPath's with rows grater then existingRows (collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)

      [indexPaths addObject:[NSIndexPath indexPathForRow:existingRows + i inSection:0]];
    

    swift version some like here:

    var indexPaths: [IndexPath] = []
    
    // find number of kittens in the data source and create their indexPaths
    var existingRows: Int = kittenDataSource.count + 1
    
    for i in 0..<moarKittens.count {
        indexPaths.append(IndexPath(row: existingRows + i, section: 0))
    }
    
    // add new kittens to the data source & notify table of new indexpaths
    kittenDataSource.append(contentsOf: moarKittens)
    if let indexPaths = indexPaths as? [IndexPath] {
        tableNode.insertRows(at: indexPaths, with: .fade)
    }