私のコレクションビューに重大な問題があります。 私は非同期をフェッチします。いくつかのデータをAPIから取得し、データ変数を設定します。私のコレクションビューには基本的にいくつかのセルが含まれています。セルの形状とレイアウトは同じなので、私は両方のために同じcollectionviewcellクラスを使用しました。UICollectionView ImageViewの問題
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count + 1
}
をそして、このように私の細胞のものを実行します:
私はこのようなアイテムの私の数を計算
class OverviewUserCell: UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var userImageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
self.makeItCircle()
}
func makeItCircle() {
self.userImageView.layer.masksToBounds = true
self.userImageView.layer.cornerRadius = CGFloat(roundf(Float(self.userImageView.frame.size.height/2.0)))
}
}
:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! OverviewUserCell
if(indexPath.row == data.count) {
cell.userImageView.image = UIImage(named: "plus")!
//some additional coloring and stuff
} else {
cell.userImageView.image = UIImage(named: "oma")!
//some additional coloring and stuff
}
cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
cell.layer.cornerRadius = 8
return cell
}
マイセル自体が丸いイメージとラベルが含まれています
どういうわけか(異なる画像サイズのためかもしれない)画像ビューのフレームが変わるなど、これはb私はそれを同じままにしておきたいと思います。私は、セル内のすべての要素に制約を追加しました。データをロードしないと、イメージは完全に丸いですが、データがロードされてコレクションビューが再ロードされると、プラスイメージはもう丸められません。
画像は同じサイズで同じ位置にする必要があります。
'makeItCircle'関数に' clipsToBounds = true'を追加してみてください。 – kye
これは動作しません。 @kye – Tobias
'CGFloat(round(Float(self.userImageView.frame.size.height/2.0)))'はあなたの問題かもしれません。丸めたり、 'CGFloat'に変換する必要はありません。 'frame.size.width/2'と' clipToBounds'を試してください – kye