garg204 / Multiple-collectionView-in-Multiple-tableView-cells

UICollectionView is embed in UITableViewCell. The collection views are horizontal scrollable. The UITableView can have a section title for each UICollectionView and the number of UICollectionView is equal to the number of sections.
25 stars 7 forks source link

Multiple UICollectionViews #1

Open garg204 opened 6 years ago

garg204 commented 6 years ago

oct-20-2017 12-12-25

Michelasso commented 6 years ago

@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. 😊