Это код представления коллекции:
Это код представления коллекции:
Код: Выделить всё
extension ProductViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return self.objectArray.count
} else if section == 1 {
return fetchingMore ? 1 : 0
}
return 0
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
var minimumLineSpacingForSectionAt: CGFloat
if section == 0 {
minimumLineSpacingForSectionAt = -50
} else {
minimumLineSpacingForSectionAt = 0
}
return minimumLineSpacingForSectionAt
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 0 {
return CGSize(width: productCollectionView.bounds.width, height: CommonService.heightForRowAtIndexPathForiPhones(cellType: .itemCell))
} else {
return CGSize(width: productCollectionView.bounds.width, height: 70)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemAlbumCollectionViewCell", for: indexPath) as! ItemAlbumCollectionViewCell
cell.setUpCell(product: objectArray[indexPath.row]!)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoadingCollectionViewCell", for: indexPath) as! LoadingCollectionViewCell
cell.activityIndicator.startAnimating()
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let productCategoryViewController = Storyboards.ProductCategoryVC.controller as! ProductCategoryViewController
productCategoryViewController.products = objectArray[indexPath.row]!
pushViewController(T: productCategoryViewController)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
guard let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "StreachyCollectionHeaderReusableView", for: indexPath) as? StreachyCollectionHeaderReusableView else { fatalError("Invalid view type") }
return headerView
default:
assert(false, "Invalid element type")
}
}
}
< /code>
и эта функция, которая требуется для получения данных: < /p>
func pagination(forDivisionCollection: String) {
fetchProduct(forDivisionCollection: forDivisionCollection, completion: { newData in
self.productCollectionView.reloadSections(IndexSet(integer: 1))
self.objectArray.append(contentsOf: newData)
self.endReached = newData.count == 0
self.fetchingMore = false
UIView.performWithoutAnimation {
self.productCollectionView.reloadData()
}
})
}
Код: Выделить всё
func pagination(isForNewDivision: Bool, forDivisionCollection: String) {
self.fetchingMore = true
if isForNewDivision == true {
self.objectArray.removeAll()
}
fetchProduct(forDivisionCollection: forDivisionCollection, completion: { newData in
self.productCollectionView.reloadSections(IndexSet(integer: 1))
self.objectArray.append(contentsOf: newData)
self.endReached = newData.count == 0
self.fetchingMore = false
UIView.performWithoutAnimation {
self.productCollectionView.reloadData()
}
})
}
Подробнее здесь: https://stackoverflow.com/questions/592 ... ll-swift-5