简体   繁体   English

滚动时UICollectionview单元格发生更改

[英]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.为了让您imageViewUICollectionViewCell round ,在Storyboard

  1. Set UICollectionViewCell's clipToBounds to true UICollectionViewCell's clipToBounds设置为true
  2. Set UIImageView's clipToBounds to true UIImageView's clipToBounds设置为true
  3. Set the aspectRatio of imageView = 1:1 设置aspectRatioimageView = 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

  1. Set the aspectRatio of imageView = 1:1 in storyboard 设置aspectRatioimageView = 1:1storyboard
  2. From your code, remove: 从您的代码中删除:

imageview.layer.masksToBounds = true

  1. In 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.

相关问题 UICollectionview加载单元格时滚动不稳定 - UICollectionview Scrolling choppy when loading cells 滚动时快速更改UICollectionView的高度 - Swift change UICollectionView height when scrolling 插入单元格时动画UICollectionView框架更改 - Animate UICollectionView frame change when inserting cells 使用带有FlowLayout的UICollectionView的单元格滚动UITableView时,应用程序崩溃 - App crashes when scrolling UITableView with cells that have UICollectionView with FlowLayout 嵌入在垂直UIScrollView中时,UICollectionView单元格无法正确滚动 - UICollectionView cells are not scrolling properly when embedded inside a vertical UIScrollView 滚动时,UICollectionView在单元格之间闪烁细长的“透视”线条 - UICollectionView flickers thin “see through” lines between cells when scrolling 滚动UICollectionView时,UICollectionView单元格在跳动 - The UICollectionView cells are jerking while scrolling the UICollectionView 单元格随UICollectionView中的水平滚动消失 - Cells disappearing with horizontal scrolling in UICollectionView UICollectionView滚动更改选定的单元格 - UICollectionView scrolling changes the selected cells 滚动后,UICollectionView对单元格进行重新排序 - UICollectionView reorder cells after scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM