[英]UICollectionview cells change when scrolling
My UICollectionview looks like this: 我的UICollectionview看起来像这样:
My UICollectionviewCell contains an Image, and by setting the corner radius property I am trying to make it round, but this is the result. 我的UICollectionviewCell包含一个图像,并且通过设置转角半径属性,我试图使其变圆,但这是结果。 The same goes for my Image in my HeaderView.
HeaderView中的Image也是如此。
Moreover do my UICollectionviewCells change when I scroll and randomly change appearance, by some finally being round, some even diamond shaped and some icons being black. 此外,当我滚动并随机更改外观时,我的UICollectionviewCells是否发生了更改,有些最终是圆形的,有些甚至是菱形的,有些图标是黑色的。
Here is the code I'm using for the CollectionviewCell: 这是我用于CollectionviewCell的代码:
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//1
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell",
for: indexPath) as! CollectionViewCell
cell.imageView.layoutIfNeeded()
cell.imageView.layer.masksToBounds = true
cell.backgroundColor = UIColor.clear
cell.imageView.tintColor = UIColor.white
cell.imageView.layer.borderWidth = 2
cell.imageView.layer.borderColor = UIColor.white.cgColor
cell.imageView.layer.cornerRadius = cell.frame.width/2
let icon = categories[indexPath.item]?.image
cell.nameLabel.text = categories[indexPath.item]?.name
cell.nameLabel.textColor = UIColor.white
cell.imageView.image = icon
let selected = categories[indexPath.item]?.selected
if selected! {
cell.isSelected = true
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
cell.imageView.backgroundColor = UIColor.white
cell.imageView.tintColor = colors.colorBottom
}
return cell
}
Here is the code of my HeaderView: 这是我的HeaderView的代码:
//make the profileImageView round
imageview.layer.masksToBounds = true
imageview.layer.cornerRadius = imageview.frame.height/2
imageview.layer.borderWidth = 4
imageview.layer.borderColor = UIColor.white.cgColor
1. To make your imageView
in UICollectionViewCell
round
, in Storyboard
: 1.为了让您
imageView
在UICollectionViewCell
round
,在Storyboard
:
UICollectionViewCell's
clipToBounds
to true
UICollectionViewCell's
clipToBounds
设置为true
UIImageView's
clipToBounds
to true
UIImageView's
clipToBounds
设置为true
aspectRatio
of imageView
= 1:1
aspectRatio
的imageView
= 1:1
Remove these 2 line from cellForItemAt
从
cellForItemAt
删除这2行
cell.imageView.layoutIfNeeded()
cell.imageView.layer.masksToBounds = true
2. Below line must be responsible for the icons becoming black
2.下一行必须负责
icons becoming black
cell.imageView.tintColor = colors.colorBottom
Check if you are changing the selected
status in categories
array whenever a cell is selected? 检查是否在每次选择单元格时都在
categories
数组中更改selected
状态?
3. In headerView
: 3.在
headerView
:
aspectRatio
of imageView
= 1:1
in storyboard
aspectRatio
的imageView
= 1:1
的storyboard
imageview.layer.masksToBounds = true
storyboard
: Set UIImageView's
clipToBounds
to true
storyboard
clipToBounds
:将UIImageView's
clipToBounds
设置为true
4. Screenshot: 4. 屏幕截图:
Let me know if you need any other kind of help regarding the code. 让我知道您是否需要有关代码的其他帮助。
您不能假定出队后重新计算了单元格的帧,请改用常量值:
cell.imageView.layer.cornerRadius = 16.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.