ios - UICollectionView 在单元格外渲染图像

标签 ios swift swift3 uicollectionview

我有一个显示 2 个单元格的 UICollectionView。每个单元格中都有一个 UIImageView。当显示单元格时,第一个单元格是正确的,但第二个单元格中的 ImageView 正在显示,就好像它在第三个单元格中一样。

enter image description here 代码:

var upgradeCollectionView: UICollectionView!

lazy var upgradeView: UIView = {

    ... //Add other views

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 1
    let itemSideSize = (self.view.frame.width - 5) / 4
    layout.itemSize = CGSize(width: itemSideSize, height: itemSideSize)
    self.upgradeCollectionView = UICollectionView(frame: CGRect(x: 0, y: 44, width: newView.frame.width, height: newView.frame.height), collectionViewLayout: layout)
    newView.addSubview(self.upgradeCollectionView)
    self.upgradeCollectionView.register(CollUpgradeCell.self, forCellWithReuseIdentifier: "CollUpgradeCell")
    self.upgradeCollectionView.delegate = self
    self.upgradeCollectionView.dataSource = self
    self.upgradeCollectionView.isHidden = true
    self.upgradeCollectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}        

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    availableUpgrades = []
    upgrades.forEach { (upgrade) in
        if upgrade.isHidden == false{
            if upgrade.owned == false{
                availableUpgrades.append(upgrade)
            }
        }
    }

    availableUpgrades = availableUpgrades.sorted(by: { $0.cost < $1.cost })

    return availableUpgrades.count //Returns 2
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollUpgradeCell", for: indexPath) as! CollUpgradeCell
    cell.resetCell() //Resets BG color, text label, and imageview.image = nil

    let upgrade = availableUpgrades[indexPath.item]

    cell.textLabel.text = String(upgrade.cost)
    cell.imageView.image = upgrade.image
    cell.backgroundColor = .green

    return cell
}

class CollUpgradeCell: UICollectionViewCell {

     var imageView = UIImageView()
     var textLabel = UILabel(frame: CGRect(x: 30, y: 30, width: 50, height: 500))

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(textLabel)
        addSubview(imageView)
        imageView.frame = frame
    }
}

最佳答案

这是您的问题,您需要将 CollectionViewCell 的边界用作 UIImageView 的框架

override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(textLabel)
        addSubview(imageView)
        //imageView.frame = frame  this is your problem source
        imageView.frame = self.bounds
    }

希望对你有帮助

关于ios - UICollectionView 在单元格外渲染图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227794/

相关文章:

ios - CGContextSetLineDash不可用

Swift 3 - 从核心数据中获取值(value)

swift - 奇怪的 tableView.reloadData() 行为

swift - 覆盖属性观察者

ios - 在 Swift 中生成图像

ios - 设备处于静音模式时播放声音

ios - 如何使用 MVVM 处理 UITableViewCell 中的 UICollectionView

ios - 如何将 UIView 限制为 UICollectionViewCell

ios - 单击 CollectionViewCell 显示先前选择的单元格而不是当前单元格

ios - NSManagedObject setter 给我 [ MyObject setName :]: unrecognized selector sent to instance