CollectionViewCell中的图像在滚动后重叠

时间:2016-11-23 05:20:22

标签: swift uicollectionview cell overlap

每当我滚动图像开始重叠。我该如何解决这个问题?

Overlapping images example

import UIKit

class ViewController: UIViewController {
    var collectionView : UICollectionView!
    let images = [#imageLiteral(resourceName: "Rune_knight"),#imageLiteral(resourceName: "Royal_Guard"),#imageLiteral(resourceName: "Guillotine_Cross"),#imageLiteral(resourceName: "Shadow_Chaaser"),#imageLiteral(resourceName: "Ranger"),#imageLiteral(resourceName: "Meastro_Wanderer"),#imageLiteral(resourceName: "Arch_Bishop"),#imageLiteral(resourceName: "Sura"),#imageLiteral(resourceName: "Warlock"),#imageLiteral(resourceName: "Sorcerer"),#imageLiteral(resourceName: "Geneticist"),#imageLiteral(resourceName: "Mechanic")]


    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // setup collection view
    func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10

        collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
        collectionView.register( JobsCollectionViewCell.self, forCellWithReuseIdentifier: "jobsCell")
        collectionView.backgroundColor = .black
        collectionView.dataSource = self
        collectionView.delegate = self

        view.addSubview(collectionView)
    }
}

extension ViewController : UICollectionViewDataSource , UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    // the number of section in collection view
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    // the number of cell in collection view
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    // definition of cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "jobsCell", for: indexPath) as! JobsCollectionViewCell
        cell.awakeFromNib() 
        return cell
    }

    // Display
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let jobsCell = cell as! JobsCollectionViewCell
        jobsCell.jobsImagesView.image = images[indexPath.row]
        jobsCell.backgroundColor = .white
    }

    // size of cell
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let sizeForItem = CGSize(width: 165, height: 165)
        return sizeForItem
    }

    // layout for cell in collection view
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let insetForSection : UIEdgeInsets
        insetForSection = UIEdgeInsetsMake(20,15,20,15)

        return insetForSection
    }
}

0 个答案:

没有答案