How to scroll to first cell, while swiping the last cell on CollectionView?












0















I'm working on a CollectionView with many cells in it. And what I am trying to do is:




  • When the user is on the last cell of collectionView, and when the user swipes to see the next cell, I want this collectionView to restarts from the beginning. ( Basically, I need an endless loop )


Hope I could explain it correctly. Waiting for your solutions. Thank you.










share|improve this question


















  • 1





    Look here How to implement horizontally infinite scrolling UICollectionView?

    – Robert Dresler
    Dec 29 '18 at 11:05













  • @Ers Tar have you find your answer from my answer?

    – Sagar koyani
    Jan 3 at 11:42











  • @Sagarkoyani yes, thank you so much. it was helpful!

    – Ers Tar
    Jan 4 at 8:47
















0















I'm working on a CollectionView with many cells in it. And what I am trying to do is:




  • When the user is on the last cell of collectionView, and when the user swipes to see the next cell, I want this collectionView to restarts from the beginning. ( Basically, I need an endless loop )


Hope I could explain it correctly. Waiting for your solutions. Thank you.










share|improve this question


















  • 1





    Look here How to implement horizontally infinite scrolling UICollectionView?

    – Robert Dresler
    Dec 29 '18 at 11:05













  • @Ers Tar have you find your answer from my answer?

    – Sagar koyani
    Jan 3 at 11:42











  • @Sagarkoyani yes, thank you so much. it was helpful!

    – Ers Tar
    Jan 4 at 8:47














0












0








0








I'm working on a CollectionView with many cells in it. And what I am trying to do is:




  • When the user is on the last cell of collectionView, and when the user swipes to see the next cell, I want this collectionView to restarts from the beginning. ( Basically, I need an endless loop )


Hope I could explain it correctly. Waiting for your solutions. Thank you.










share|improve this question














I'm working on a CollectionView with many cells in it. And what I am trying to do is:




  • When the user is on the last cell of collectionView, and when the user swipes to see the next cell, I want this collectionView to restarts from the beginning. ( Basically, I need an endless loop )


Hope I could explain it correctly. Waiting for your solutions. Thank you.







ios swift uicollectionview uicollectionviewcell collectionview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 29 '18 at 11:02









Ers TarErs Tar

487




487








  • 1





    Look here How to implement horizontally infinite scrolling UICollectionView?

    – Robert Dresler
    Dec 29 '18 at 11:05













  • @Ers Tar have you find your answer from my answer?

    – Sagar koyani
    Jan 3 at 11:42











  • @Sagarkoyani yes, thank you so much. it was helpful!

    – Ers Tar
    Jan 4 at 8:47














  • 1





    Look here How to implement horizontally infinite scrolling UICollectionView?

    – Robert Dresler
    Dec 29 '18 at 11:05













  • @Ers Tar have you find your answer from my answer?

    – Sagar koyani
    Jan 3 at 11:42











  • @Sagarkoyani yes, thank you so much. it was helpful!

    – Ers Tar
    Jan 4 at 8:47








1




1





Look here How to implement horizontally infinite scrolling UICollectionView?

– Robert Dresler
Dec 29 '18 at 11:05







Look here How to implement horizontally infinite scrolling UICollectionView?

– Robert Dresler
Dec 29 '18 at 11:05















@Ers Tar have you find your answer from my answer?

– Sagar koyani
Jan 3 at 11:42





@Ers Tar have you find your answer from my answer?

– Sagar koyani
Jan 3 at 11:42













@Sagarkoyani yes, thank you so much. it was helpful!

– Ers Tar
Jan 4 at 8:47





@Sagarkoyani yes, thank you so much. it was helpful!

– Ers Tar
Jan 4 at 8:47












1 Answer
1






active

oldest

votes


















0














Try this.



import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate=self;
collectionView.dataSource=self;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
if indexpath.row==99{
self.btnScroll()
}
return cell
}

func btnScroll() {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
}
}





share|improve this answer


























  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

    – Ers Tar
    Jan 4 at 8:48











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',
autoActivateHeartbeat: false,
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%2f53968933%2fhow-to-scroll-to-first-cell-while-swiping-the-last-cell-on-collectionview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try this.



import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate=self;
collectionView.dataSource=self;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
if indexpath.row==99{
self.btnScroll()
}
return cell
}

func btnScroll() {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
}
}





share|improve this answer


























  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

    – Ers Tar
    Jan 4 at 8:48
















0














Try this.



import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate=self;
collectionView.dataSource=self;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
if indexpath.row==99{
self.btnScroll()
}
return cell
}

func btnScroll() {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
}
}





share|improve this answer


























  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

    – Ers Tar
    Jan 4 at 8:48














0












0








0







Try this.



import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate=self;
collectionView.dataSource=self;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
if indexpath.row==99{
self.btnScroll()
}
return cell
}

func btnScroll() {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
}
}





share|improve this answer















Try this.



import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate=self;
collectionView.dataSource=self;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
if indexpath.row==99{
self.btnScroll()
}
return cell
}

func btnScroll() {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 14:01

























answered Dec 29 '18 at 13:05









Sagar koyaniSagar koyani

1339




1339













  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

    – Ers Tar
    Jan 4 at 8:48



















  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

    – Ers Tar
    Jan 4 at 8:48

















unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

– Ers Tar
Jan 4 at 8:48





unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView

– Ers Tar
Jan 4 at 8:48


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968933%2fhow-to-scroll-to-first-cell-while-swiping-the-last-cell-on-collectionview%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas