Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple UICollectionViews #1

Open
garg204 opened this issue Oct 20, 2017 · 1 comment
Open

Multiple UICollectionViews #1

garg204 opened this issue Oct 20, 2017 · 1 comment

Comments

@garg204
Copy link
Owner

garg204 commented Oct 20, 2017

oct-20-2017 12-12-25

@Michelasso
Copy link

@garg204 That's because when you pick a random number from 0..<10 the same number can be chosen multiple times. One way to fix it is to to shuffle the array of images in advance. In my case I use Xcode 10 beta with Swift 4.2 so this is how I modified the code:

imageArray = ["1.jpeg","2.jpeg","3.jpeg","4.jpeg","5.jpeg","6.jpeg","7.jpeg","8.jpeg","9.jpeg","10.jpeg"].shuffled()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionViewCell
        {
//            let randomNumber = Int(arc4random_uniform(UInt32(imageArray.count)))
            cell.imageView.image = UIImage(named: imageArray[indexPath.row])
            return cell
        }
        return UICollectionViewCell()
    }

For previous Swift version you may check: https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift

Also I had layout issues due to scrollbars. I disabled all "Show vertical/horizontal indicator" (also because it actually looks better) and the warnings are gone.

Apart from that thanks for the great tutorial! Quite instructive. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants