problem with images loading on top of already set images swift 4

痞子三分冷 提交于 2019-12-20 06:24:03

问题


I'm having an issue in my cellForItemAtIndexPath where I am setting an image to my cell's UIButton but every time I scroll the collectionView's cells, it's placing the image on top of the already set image again and again. I can tell because the shadow of the image is getting thicker and thicker. I'm pulling the images from an array that I created of image literals in that swift file and the correct images are loading so there's no problem there. I'm sure this is a simple fix for most but I can't seem to find an answer anywhere.

Image of my cellForItemAtIndexPath function

My app running before I scroll

App after scrolling a bit

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeViewCell

    collectionView.bounces = false

    let imageNumber = indexPath.item

    let collectionImage: UIButton = {
        let image = UIButton()
        image.setImage(collectionImageArray[imageNumber].withRenderingMode(.alwaysOriginal), for: .normal)
        image.contentMode = .scaleAspectFit
        image.addTarget(self, action: #selector(handleCollectionTap), for: .touchUpInside)
        return image
    }()

    collectionImage.imageView?.image = collectionImageArray[imageNumber]

    cell.addSubview(collectionImage)
    collectionImage.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    collectionImage.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
    collectionImage.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true


    print(imageNumber)

    return cell
}

回答1:


If anyone else comes across this issue (or maybe I'm the only dumb one), the problem was that I should have created my UIButton, added the subview, and constrained it inside of the cell class and from the cellForItem AtindexPath method set the image and target handler like this.

class HomeViewCell: UICollectionViewCell {

let collectionImage: UIButton = {
    let image = UIButton(type: .custom)
    image.contentMode = .scaleAspectFit
    return image
}()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(collectionImage)
    collectionImage.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    collectionImage.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    collectionImage.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeViewCell

    collectionView.bounces = false

    let imageNumber = indexPath.item

    let image = collectionImageArray[imageNumber].withRenderingMode(.alwaysOriginal)
    cell.collectionImage.setImage(image, for: .normal)
    cell.collectionImage.addTarget(self, action: #selector(handleCollectionTap), for: .touchUpInside)

    print(imageNumber)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height)
}

@objc func handleCollectionTap() {
    let layout = UICollectionViewFlowLayout()
    let cardViewer = CardViewerController(collectionViewLayout: layout)
    present(cardViewer, animated: true, completion: nil)
}

Everything is running smoothly now! :)



来源:https://stackoverflow.com/questions/53453336/problem-with-images-loading-on-top-of-already-set-images-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!