james james - 2 months ago 19
Swift Question

(Swift) How can you assign specific number of items for separate iCarousels?

This iCarousel: https://github.com/nicklockwood/iCarousel

He has a 'multiple carousels' example project that is in Obj-C, however the number of items is random.

In the number of item function, I am unable to explicitly state which iCarousel I wish to assign a number of items too. They always seem to load in a random order. I have 6 BTW.

Here are pictures of the outlets.

StoryBoard connections

ViewController connection

The rest of my code is identical to yours.
It cannon reference the carouselView in the numberOfItems function.

My views: views

Data source: data source

Answer

The iCarousel instance is passed as a parameter to the numberOfItemsInCarousel function. You can use this to determine which carousel is requesting the number of items in each case. Something like:

class ViewController: UIViewController, iCarouselDataSource {
    @IBOutlet weak var carousel1: iCarousel!
    @IBOutelt weak var carousel2: iCarousel!

    var carousel1Items = [String]()
    var carousel2Items = [String]()

    func numberOfItemsInCarousel(carousel: iCarousel) -> Int {

        var numberOfItems = 0

        switch carousel {
            case self.carousel1:
                numberOfItems = self.carousel1Items.count

            case self.carousel2:
                numberOfItems = self.carousel2Items.count

            default:
                print("Unknown carousel!")
        }

        return numberOfItems
    }
} 
Comments