繁体   English   中英

在已设置的图像上加载图像的问题swift 4

[英]problem with images loading on top of already set images swift 4

我在cellForItemAtIndexPath中遇到一个问题,该问题是我在单元格的UIButton中设置图像,但每次滚动collectionView的单元格时,都会一次又一次地将图像放置在已设置的图像之上。 我可以知道,因为图像的阴影越来越浓。 我正在从该swift文件中的图像文字创建的数组中拉出图像,并且正在加载正确的图像,因此在那里没有问题。 我相信这对大多数人来说都是简单的解决方法,但是我似乎找不到任何答案。

我的cellForItemAtIndexPath函数的图像

滚动之前我的应用程序正在运行

滚动一点后的应用

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
}

如果还有其他人遇到此问题(或者也许我是唯一一个愚蠢的人),那么问题是我应该创建我的UIButton,添加子视图,并将其约束在单元格类内部,并从cellForItem AtindexPath方法设置为图像和目标处理程序是这样的。

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)
}

现在一切运行顺利! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM