0

我正在使用 ASCollectionNode 水平滚动,我正在尝试向左存档滚动以加载更多并插入项目。但是当我到达左侧的最后一项时,项目被插入左侧而不是右侧。

我实现了这样的插入..

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

1 回答 1

0

要在列表末尾添加单元格,您必须创建 IndexPath 的数组,其行数大于现有行数(collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)

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

快速版本有些像这里:

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)
}
于 2019-02-24T23:28:51.437 回答