2017-07-21 8 views
0

2つのセルが表示されたUICollectionViewがあります。各セルにはUIImageViewがあります。セルが表示されると、最初のセルは正しく表示されますが、2番目のセルのImageViewは3番目のセルのように表示されます。セル外のUICollectionViewレンダリングイメージ

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 
    } 
} 
+0

あなたのセルを助け、あなたのCollectionViewCell

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

のごUIImageView境界希望のフレームとして使用する必要があります。 imageViewは自動レイアウトを使用しますか?あなたはそれに制約を使用していますか? –

+0

あなたは 'resetCell()'で何をしていても、 'prepareForReuse()'でそれを行うことができます –

+0

私はセル内のImageViewをどのようにセットアップするかを示す質問を編集しました –

答えて

2

があなたの問題であり、あなたはこれが

+0

これはトリックでした。ありがとう –

関連する問題