UICollectionView not Paginating Vertically











up vote
0
down vote

favorite












I am working on an application where I get 100 data from my backend API, I am displaying this data in a collectionView but displaying 100 data at once could be very long and annoying so I want to implement pagination to the UICollectionView, through my research, I tried implementing some of the code line I got but when ever I scroll up, I see nothing. No UIActivityIndicator shows nothing. I have searched for this for about three straight days but nothing positive. I have pasted my code below with hopes of someone helping with it. Thanks in advance



class MainVC: UIViewController {

@IBOutlet weak var collectionView: UICollectionView!
@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {}
@IBOutlet weak var spinner: UIActivityIndicatorView!

var itemArray = OfflineFunctions.instance.getDataFromDB()
var flickrArray = ServiceProvider.instance.flickerPhotos // This returns 100 items

var offline: Bool = false
var isLoadMore: Bool = false
var isLastPageReached: Bool = false
var limit = 10
let totalEntries = ServiceProvider.instance.flickerPhotos.count // This returns 100 items

override func viewDidLoad() {
super.viewDidLoad()

collectionView.delegate = self
collectionView.dataSource = self
setupWithPhotos()
collectionView.isPagingEnabled = true

}


override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
collectionView.reloadData()
}

// Function that gets the items

func setupWithPhotos() -> Void {

spinner.isHidden = false
spinner.startAnimating()

ServiceProvider.instance.getPhotos {[weak self] (success, error) in
if success {
self?.collectionView.reloadData()
self?.spinner.isHidden = true
self?.spinner.stopAnimating()

self?.offline = false
}

if error != nil {

alert.addAction(UIAlertAction(title: "Ok", style: .cancel) { (action:UIAlertAction!) in
self?.spinner.isHidden = true
self?.spinner.stopAnimating()
self?.collectionView.reloadData()
self?.offline = true
})

self?.present(alert, animated: true)
}
}
}

@IBAction func reloadPressed(_ sender: Any) {
setupWithPhotos()
}

@IBAction func closePressed(_ sender: Any) {
performSegue(withIdentifier: UNWIND, sender: nil)
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
targetContentOffset.pointee = scrollView.contentOffset
var indexes = self.collectionView.indexPathsForVisibleItems
indexes.sort()
var index = indexes.first!
let cell = self.collectionView.cellForItem(at: index)!
let position = self.collectionView.contentOffset.y - cell.frame.origin.y
if position > cell.frame.size.height/2{
index.row = index.row+1
}
self.collectionView.scrollToItem(at: index, at: .left, animated: true )
}

}

extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {

cell.offline = offline
if !offline {

let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]
cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
return cell
} else {

let flickerPhoto = itemArray[indexPath.row]
cell.configueOfflineCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
return cell
}
}
return MainCell()
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if !offline {
return ServiceProvider.instance.flickerPhotos.count
} else {
return itemArray.count
}

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// setting cells based on screen size

var numOfColums : CGFloat = 2
if UIScreen.main.bounds.width > 320 {
numOfColums = 1
}

let spaceBtwCells : CGFloat = 10
let padding: CGFloat = 40
let cellDimension = ((collectionView.bounds.width - padding) - (numOfColums - 1) * spaceBtwCells) / numOfColums

return CGSize(width: cellDimension, height: cellDimension)

}



func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
referenceSizeForFooterInSection section: Int) -> CGSize {

if flickrArray.count > 0 && isLastPageReached == false {
return CGSize(width:(collectionView.frame.size.width), height: 100.0)
}
return CGSize.zero

}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

if kind == UICollectionView.elementKindSectionFooter {

let vew = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footer", for: indexPath)


let loading = UIActivityIndicatorView()
loading.style = .gray
loading.translatesAutoresizingMaskIntoConstraints = false
loading.tintColor = UIColor.gray
loading.tag = -123456
vew.addSubview(loading)
vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerX, relatedBy: .equal, toItem: vew, attribute: .centerX, multiplier: 1, constant: 0))
vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerY, relatedBy: .equal, toItem: vew, attribute: .centerY, multiplier: 1, constant: 0))


return vew
}
return UICollectionReusableView()
}

func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
if elementKind == UICollectionView.elementKindSectionFooter {

if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
if flickrArray.count > 0 && isLastPageReached == false {
loadingView.startAnimating()
print("END REACH 1")
}
else {
self.isLoadMore = false
}
}
}
}

func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
if elementKind == UICollectionView.elementKindSectionFooter{

if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
loadingView.stopAnimating()
loadingView.removeFromSuperview()

self.isLoadMore = false
}

}
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


if !offline {
let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.item]
ServiceProvider.instance.selectedPhoto = flickerPhoto
performSegue(withIdentifier: DETAIL_VC_SEGUE, sender:indexPath)
} else {
let offlineList = OfflineFunctions.instance.offlineItems?[indexPath.item]
OfflineFunctions.instance.selectedImage = offlineList
performSegue(withIdentifier: MAIN_TO_SAVED_DETAIL_VC_SEGUE, sender:indexPath)
}
NotificationCenter.default.post(name: NOTIFY_PHOTO_SELECTED, object: nil)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == SAVED_DETAIL_VC) {

let popUp = segue.destination as! SavedDetailVC
popUp.doneSaving = { [weak self] in
self?.collectionView.reloadData()
}

}
}

}


here is a link to my API get https://www.flickr.com/services/api/explore/flickr.photos.getRecent
further codes would be added on request










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am working on an application where I get 100 data from my backend API, I am displaying this data in a collectionView but displaying 100 data at once could be very long and annoying so I want to implement pagination to the UICollectionView, through my research, I tried implementing some of the code line I got but when ever I scroll up, I see nothing. No UIActivityIndicator shows nothing. I have searched for this for about three straight days but nothing positive. I have pasted my code below with hopes of someone helping with it. Thanks in advance



    class MainVC: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBAction func prepareForUnwind(segue: UIStoryboardSegue) {}
    @IBOutlet weak var spinner: UIActivityIndicatorView!

    var itemArray = OfflineFunctions.instance.getDataFromDB()
    var flickrArray = ServiceProvider.instance.flickerPhotos // This returns 100 items

    var offline: Bool = false
    var isLoadMore: Bool = false
    var isLastPageReached: Bool = false
    var limit = 10
    let totalEntries = ServiceProvider.instance.flickerPhotos.count // This returns 100 items

    override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.delegate = self
    collectionView.dataSource = self
    setupWithPhotos()
    collectionView.isPagingEnabled = true

    }


    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    collectionView.reloadData()
    }

    // Function that gets the items

    func setupWithPhotos() -> Void {

    spinner.isHidden = false
    spinner.startAnimating()

    ServiceProvider.instance.getPhotos {[weak self] (success, error) in
    if success {
    self?.collectionView.reloadData()
    self?.spinner.isHidden = true
    self?.spinner.stopAnimating()

    self?.offline = false
    }

    if error != nil {

    alert.addAction(UIAlertAction(title: "Ok", style: .cancel) { (action:UIAlertAction!) in
    self?.spinner.isHidden = true
    self?.spinner.stopAnimating()
    self?.collectionView.reloadData()
    self?.offline = true
    })

    self?.present(alert, animated: true)
    }
    }
    }

    @IBAction func reloadPressed(_ sender: Any) {
    setupWithPhotos()
    }

    @IBAction func closePressed(_ sender: Any) {
    performSegue(withIdentifier: UNWIND, sender: nil)
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.pointee = scrollView.contentOffset
    var indexes = self.collectionView.indexPathsForVisibleItems
    indexes.sort()
    var index = indexes.first!
    let cell = self.collectionView.cellForItem(at: index)!
    let position = self.collectionView.contentOffset.y - cell.frame.origin.y
    if position > cell.frame.size.height/2{
    index.row = index.row+1
    }
    self.collectionView.scrollToItem(at: index, at: .left, animated: true )
    }

    }

    extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {

    cell.offline = offline
    if !offline {

    let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]
    cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
    return cell
    } else {

    let flickerPhoto = itemArray[indexPath.row]
    cell.configueOfflineCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
    return cell
    }
    }
    return MainCell()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if !offline {
    return ServiceProvider.instance.flickerPhotos.count
    } else {
    return itemArray.count
    }

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // setting cells based on screen size

    var numOfColums : CGFloat = 2
    if UIScreen.main.bounds.width > 320 {
    numOfColums = 1
    }

    let spaceBtwCells : CGFloat = 10
    let padding: CGFloat = 40
    let cellDimension = ((collectionView.bounds.width - padding) - (numOfColums - 1) * spaceBtwCells) / numOfColums

    return CGSize(width: cellDimension, height: cellDimension)

    }



    func collectionView(_ collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
    referenceSizeForFooterInSection section: Int) -> CGSize {

    if flickrArray.count > 0 && isLastPageReached == false {
    return CGSize(width:(collectionView.frame.size.width), height: 100.0)
    }
    return CGSize.zero

    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    if kind == UICollectionView.elementKindSectionFooter {

    let vew = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footer", for: indexPath)


    let loading = UIActivityIndicatorView()
    loading.style = .gray
    loading.translatesAutoresizingMaskIntoConstraints = false
    loading.tintColor = UIColor.gray
    loading.tag = -123456
    vew.addSubview(loading)
    vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerX, relatedBy: .equal, toItem: vew, attribute: .centerX, multiplier: 1, constant: 0))
    vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerY, relatedBy: .equal, toItem: vew, attribute: .centerY, multiplier: 1, constant: 0))


    return vew
    }
    return UICollectionReusableView()
    }

    func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
    if elementKind == UICollectionView.elementKindSectionFooter {

    if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
    if flickrArray.count > 0 && isLastPageReached == false {
    loadingView.startAnimating()
    print("END REACH 1")
    }
    else {
    self.isLoadMore = false
    }
    }
    }
    }

    func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
    if elementKind == UICollectionView.elementKindSectionFooter{

    if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
    loadingView.stopAnimating()
    loadingView.removeFromSuperview()

    self.isLoadMore = false
    }

    }
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    if !offline {
    let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.item]
    ServiceProvider.instance.selectedPhoto = flickerPhoto
    performSegue(withIdentifier: DETAIL_VC_SEGUE, sender:indexPath)
    } else {
    let offlineList = OfflineFunctions.instance.offlineItems?[indexPath.item]
    OfflineFunctions.instance.selectedImage = offlineList
    performSegue(withIdentifier: MAIN_TO_SAVED_DETAIL_VC_SEGUE, sender:indexPath)
    }
    NotificationCenter.default.post(name: NOTIFY_PHOTO_SELECTED, object: nil)


    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == SAVED_DETAIL_VC) {

    let popUp = segue.destination as! SavedDetailVC
    popUp.doneSaving = { [weak self] in
    self?.collectionView.reloadData()
    }

    }
    }

    }


    here is a link to my API get https://www.flickr.com/services/api/explore/flickr.photos.getRecent
    further codes would be added on request










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am working on an application where I get 100 data from my backend API, I am displaying this data in a collectionView but displaying 100 data at once could be very long and annoying so I want to implement pagination to the UICollectionView, through my research, I tried implementing some of the code line I got but when ever I scroll up, I see nothing. No UIActivityIndicator shows nothing. I have searched for this for about three straight days but nothing positive. I have pasted my code below with hopes of someone helping with it. Thanks in advance



      class MainVC: UIViewController {

      @IBOutlet weak var collectionView: UICollectionView!
      @IBAction func prepareForUnwind(segue: UIStoryboardSegue) {}
      @IBOutlet weak var spinner: UIActivityIndicatorView!

      var itemArray = OfflineFunctions.instance.getDataFromDB()
      var flickrArray = ServiceProvider.instance.flickerPhotos // This returns 100 items

      var offline: Bool = false
      var isLoadMore: Bool = false
      var isLastPageReached: Bool = false
      var limit = 10
      let totalEntries = ServiceProvider.instance.flickerPhotos.count // This returns 100 items

      override func viewDidLoad() {
      super.viewDidLoad()

      collectionView.delegate = self
      collectionView.dataSource = self
      setupWithPhotos()
      collectionView.isPagingEnabled = true

      }


      override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      collectionView.reloadData()
      }

      // Function that gets the items

      func setupWithPhotos() -> Void {

      spinner.isHidden = false
      spinner.startAnimating()

      ServiceProvider.instance.getPhotos {[weak self] (success, error) in
      if success {
      self?.collectionView.reloadData()
      self?.spinner.isHidden = true
      self?.spinner.stopAnimating()

      self?.offline = false
      }

      if error != nil {

      alert.addAction(UIAlertAction(title: "Ok", style: .cancel) { (action:UIAlertAction!) in
      self?.spinner.isHidden = true
      self?.spinner.stopAnimating()
      self?.collectionView.reloadData()
      self?.offline = true
      })

      self?.present(alert, animated: true)
      }
      }
      }

      @IBAction func reloadPressed(_ sender: Any) {
      setupWithPhotos()
      }

      @IBAction func closePressed(_ sender: Any) {
      performSegue(withIdentifier: UNWIND, sender: nil)
      }

      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
      targetContentOffset.pointee = scrollView.contentOffset
      var indexes = self.collectionView.indexPathsForVisibleItems
      indexes.sort()
      var index = indexes.first!
      let cell = self.collectionView.cellForItem(at: index)!
      let position = self.collectionView.contentOffset.y - cell.frame.origin.y
      if position > cell.frame.size.height/2{
      index.row = index.row+1
      }
      self.collectionView.scrollToItem(at: index, at: .left, animated: true )
      }

      }

      extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {

      cell.offline = offline
      if !offline {

      let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]
      cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
      return cell
      } else {

      let flickerPhoto = itemArray[indexPath.row]
      cell.configueOfflineCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
      return cell
      }
      }
      return MainCell()
      }

      func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 1
      }

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      if !offline {
      return ServiceProvider.instance.flickerPhotos.count
      } else {
      return itemArray.count
      }

      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      // setting cells based on screen size

      var numOfColums : CGFloat = 2
      if UIScreen.main.bounds.width > 320 {
      numOfColums = 1
      }

      let spaceBtwCells : CGFloat = 10
      let padding: CGFloat = 40
      let cellDimension = ((collectionView.bounds.width - padding) - (numOfColums - 1) * spaceBtwCells) / numOfColums

      return CGSize(width: cellDimension, height: cellDimension)

      }



      func collectionView(_ collectionView: UICollectionView, layout
      collectionViewLayout: UICollectionViewLayout,
      referenceSizeForFooterInSection section: Int) -> CGSize {

      if flickrArray.count > 0 && isLastPageReached == false {
      return CGSize(width:(collectionView.frame.size.width), height: 100.0)
      }
      return CGSize.zero

      }

      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

      if kind == UICollectionView.elementKindSectionFooter {

      let vew = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footer", for: indexPath)


      let loading = UIActivityIndicatorView()
      loading.style = .gray
      loading.translatesAutoresizingMaskIntoConstraints = false
      loading.tintColor = UIColor.gray
      loading.tag = -123456
      vew.addSubview(loading)
      vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerX, relatedBy: .equal, toItem: vew, attribute: .centerX, multiplier: 1, constant: 0))
      vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerY, relatedBy: .equal, toItem: vew, attribute: .centerY, multiplier: 1, constant: 0))


      return vew
      }
      return UICollectionReusableView()
      }

      func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
      if elementKind == UICollectionView.elementKindSectionFooter {

      if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
      if flickrArray.count > 0 && isLastPageReached == false {
      loadingView.startAnimating()
      print("END REACH 1")
      }
      else {
      self.isLoadMore = false
      }
      }
      }
      }

      func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
      if elementKind == UICollectionView.elementKindSectionFooter{

      if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
      loadingView.stopAnimating()
      loadingView.removeFromSuperview()

      self.isLoadMore = false
      }

      }
      }

      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


      if !offline {
      let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.item]
      ServiceProvider.instance.selectedPhoto = flickerPhoto
      performSegue(withIdentifier: DETAIL_VC_SEGUE, sender:indexPath)
      } else {
      let offlineList = OfflineFunctions.instance.offlineItems?[indexPath.item]
      OfflineFunctions.instance.selectedImage = offlineList
      performSegue(withIdentifier: MAIN_TO_SAVED_DETAIL_VC_SEGUE, sender:indexPath)
      }
      NotificationCenter.default.post(name: NOTIFY_PHOTO_SELECTED, object: nil)


      }

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if (segue.identifier == SAVED_DETAIL_VC) {

      let popUp = segue.destination as! SavedDetailVC
      popUp.doneSaving = { [weak self] in
      self?.collectionView.reloadData()
      }

      }
      }

      }


      here is a link to my API get https://www.flickr.com/services/api/explore/flickr.photos.getRecent
      further codes would be added on request










      share|improve this question















      I am working on an application where I get 100 data from my backend API, I am displaying this data in a collectionView but displaying 100 data at once could be very long and annoying so I want to implement pagination to the UICollectionView, through my research, I tried implementing some of the code line I got but when ever I scroll up, I see nothing. No UIActivityIndicator shows nothing. I have searched for this for about three straight days but nothing positive. I have pasted my code below with hopes of someone helping with it. Thanks in advance



      class MainVC: UIViewController {

      @IBOutlet weak var collectionView: UICollectionView!
      @IBAction func prepareForUnwind(segue: UIStoryboardSegue) {}
      @IBOutlet weak var spinner: UIActivityIndicatorView!

      var itemArray = OfflineFunctions.instance.getDataFromDB()
      var flickrArray = ServiceProvider.instance.flickerPhotos // This returns 100 items

      var offline: Bool = false
      var isLoadMore: Bool = false
      var isLastPageReached: Bool = false
      var limit = 10
      let totalEntries = ServiceProvider.instance.flickerPhotos.count // This returns 100 items

      override func viewDidLoad() {
      super.viewDidLoad()

      collectionView.delegate = self
      collectionView.dataSource = self
      setupWithPhotos()
      collectionView.isPagingEnabled = true

      }


      override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      collectionView.reloadData()
      }

      // Function that gets the items

      func setupWithPhotos() -> Void {

      spinner.isHidden = false
      spinner.startAnimating()

      ServiceProvider.instance.getPhotos {[weak self] (success, error) in
      if success {
      self?.collectionView.reloadData()
      self?.spinner.isHidden = true
      self?.spinner.stopAnimating()

      self?.offline = false
      }

      if error != nil {

      alert.addAction(UIAlertAction(title: "Ok", style: .cancel) { (action:UIAlertAction!) in
      self?.spinner.isHidden = true
      self?.spinner.stopAnimating()
      self?.collectionView.reloadData()
      self?.offline = true
      })

      self?.present(alert, animated: true)
      }
      }
      }

      @IBAction func reloadPressed(_ sender: Any) {
      setupWithPhotos()
      }

      @IBAction func closePressed(_ sender: Any) {
      performSegue(withIdentifier: UNWIND, sender: nil)
      }

      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
      targetContentOffset.pointee = scrollView.contentOffset
      var indexes = self.collectionView.indexPathsForVisibleItems
      indexes.sort()
      var index = indexes.first!
      let cell = self.collectionView.cellForItem(at: index)!
      let position = self.collectionView.contentOffset.y - cell.frame.origin.y
      if position > cell.frame.size.height/2{
      index.row = index.row+1
      }
      self.collectionView.scrollToItem(at: index, at: .left, animated: true )
      }

      }

      extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {

      cell.offline = offline
      if !offline {

      let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]
      cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
      return cell
      } else {

      let flickerPhoto = itemArray[indexPath.row]
      cell.configueOfflineCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
      return cell
      }
      }
      return MainCell()
      }

      func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 1
      }

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      if !offline {
      return ServiceProvider.instance.flickerPhotos.count
      } else {
      return itemArray.count
      }

      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      // setting cells based on screen size

      var numOfColums : CGFloat = 2
      if UIScreen.main.bounds.width > 320 {
      numOfColums = 1
      }

      let spaceBtwCells : CGFloat = 10
      let padding: CGFloat = 40
      let cellDimension = ((collectionView.bounds.width - padding) - (numOfColums - 1) * spaceBtwCells) / numOfColums

      return CGSize(width: cellDimension, height: cellDimension)

      }



      func collectionView(_ collectionView: UICollectionView, layout
      collectionViewLayout: UICollectionViewLayout,
      referenceSizeForFooterInSection section: Int) -> CGSize {

      if flickrArray.count > 0 && isLastPageReached == false {
      return CGSize(width:(collectionView.frame.size.width), height: 100.0)
      }
      return CGSize.zero

      }

      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

      if kind == UICollectionView.elementKindSectionFooter {

      let vew = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footer", for: indexPath)


      let loading = UIActivityIndicatorView()
      loading.style = .gray
      loading.translatesAutoresizingMaskIntoConstraints = false
      loading.tintColor = UIColor.gray
      loading.tag = -123456
      vew.addSubview(loading)
      vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerX, relatedBy: .equal, toItem: vew, attribute: .centerX, multiplier: 1, constant: 0))
      vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerY, relatedBy: .equal, toItem: vew, attribute: .centerY, multiplier: 1, constant: 0))


      return vew
      }
      return UICollectionReusableView()
      }

      func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
      if elementKind == UICollectionView.elementKindSectionFooter {

      if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
      if flickrArray.count > 0 && isLastPageReached == false {
      loadingView.startAnimating()
      print("END REACH 1")
      }
      else {
      self.isLoadMore = false
      }
      }
      }
      }

      func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
      if elementKind == UICollectionView.elementKindSectionFooter{

      if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
      loadingView.stopAnimating()
      loadingView.removeFromSuperview()

      self.isLoadMore = false
      }

      }
      }

      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


      if !offline {
      let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.item]
      ServiceProvider.instance.selectedPhoto = flickerPhoto
      performSegue(withIdentifier: DETAIL_VC_SEGUE, sender:indexPath)
      } else {
      let offlineList = OfflineFunctions.instance.offlineItems?[indexPath.item]
      OfflineFunctions.instance.selectedImage = offlineList
      performSegue(withIdentifier: MAIN_TO_SAVED_DETAIL_VC_SEGUE, sender:indexPath)
      }
      NotificationCenter.default.post(name: NOTIFY_PHOTO_SELECTED, object: nil)


      }

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if (segue.identifier == SAVED_DETAIL_VC) {

      let popUp = segue.destination as! SavedDetailVC
      popUp.doneSaving = { [weak self] in
      self?.collectionView.reloadData()
      }

      }
      }

      }


      here is a link to my API get https://www.flickr.com/services/api/explore/flickr.photos.getRecent
      further codes would be added on request







      ios swift uicollectionview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 7:02

























      asked Nov 19 at 6:41









      King

      196118




      196118





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369529%2fuicollectionview-not-paginating-vertically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369529%2fuicollectionview-not-paginating-vertically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy