swift iCarousel is not loading in the correct order

239 views Asked by At

I have integrated the carousel library in my application, when the page is launched I will hit one API after getting the response I will reload the carousel

here the carousel delegate method is not calling in the correct order

func numberOfItems(in carousel: iCarousel) -> Int {
        return 100
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
print("carousel viewForItemAt index:  \(index)")

let view = Bundle.main.loadNibNamed("StatusCarouselCell", owner: self, options: nil)![0] as! StatusCarouselCell
return view

}

result :

carousel viewForItemAt index: 0
carousel viewForItemAt index: 3
carousel viewForItemAt index: 2
carousel viewForItemAt index: 1

it's loading the order like 0, 3, 2, 1 index, I am not sure if it's related to setting or something, I want the carousel should load in the ascending order

Can anyone please suggest a way to fix this issue?

Thanks

1

There are 1 answers

0
Duncan C On

As Paul said in his comment, the order in which cells are requested is not defined. You should not make any assumptions about the order in which the cells will be requested, and should simply provide any cell that is requested, even if it is requested repeatedly. This is true of iCarousel, as well as the Apple UITableView and UICollectionView classes.

You said "...for example, tableView will load like indexpath.row =0,1, 2, 3, 4... this is the correct order..." That is a wrong assumption. If, tomorrow, your table view started requesting cells in the order row = [3, 0, 1, 4, 2] that would be perfectly valid, and within the acceptable behavior of a table view. If you write a program that relies on cells being requested in a specific order, your program is fragile and dependent on undocumented behavior of the framework that is not guaranteed.

What matters is the order in which the cells are shown on-screen. If you add numeric labels to your cells, you will find that they are displayed in index order. (iCarousel has quite a few different cell layouts, and the definition of "index order" varies between layouts.)