以编程方式更改UICollectionView中的第一项

我目前渲染10个彩色圆圈,您可以水平滑动以选择它们。我想 以编程方式更改UICollectionView中的第一项,并在第一项中添加照相机的小图片。绑定的集合只是RGB值的数组。我将第一个设置为全0,因此它是一种独特的颜色,因为它将永远不会被真正使用。

就第一项而言,我不希望它成为在视图中滚动的第一项,而是在您无法再滚动并且位于集合中的字面第一项时。

func collectionView(_ collectionView: UICollectionView,willDisplay: UICollectionViewCell,forItemAt: IndexPath) {
...
...

//Define the first item as the indexpath 0,0
let firstItem:IndexPath = IndexPath(row: 0,section: 0)


if (forItemAt == myIndexPath)
 {
   coloredCircle.addSubview(cameraImageView!) 
 }
else {
   cameraImageView!.removeFromSuperview()   
 }

因此,在上面,我的想法是我要检查是否有第一项显示在视图中,然后将其添加到子视图中,否则我将其拿走(因为它似乎以某种奇怪的方式堆积了起来)

无论哪种方式,尝试它都会表现出一些奇怪的行为,其中第一项将出现并显示照相机,但是一旦我停止滚动,它就会消失。

因此,如果我缓慢滚动以使第一项不在视野中,然后再缓慢滚动回去,它将显示并正常工作!

以编程方式更改UICollectionView中的第一项

但是一旦我向左滚动直到我不能走了,或者它滚动得很快,它就消失了:

以编程方式更改UICollectionView中的第一项

我有什么建议或方法可以做到,因此第一个圆圈永久是一个小照相机?

weiche 回答:以编程方式更改UICollectionView中的第一项

您不需要将逻辑放在willDisplay内,而是可以在彩色圆圈内使用UIImageView并在indexPath的基础上隐藏/取消隐藏它。另外,将相机图像放入ImageView中。

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let firstItem:IndexPath = IndexPath(row: 0,section: 0)
    if indexPath == firstItem {
        coloredCircle.imageView.isHidden = flase
    }else{
        coloredCircle.imageView.isHidden = true
    }

}
,

您可以制作2个不同的单元格。 1个单元格将包含相机视图,其他单元格将包含彩色视图。

在CellforRow方法中,您可以根据需要返回适当的单元格。

如果这不是您想要的,请提前抱歉。因为这是我第一次回答任何问题。 :)

,

请添加这些委托,数据源和FlowLayout委托

UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

为收藏视图创建出口

@IBOutlet weak var currentCollectionView: UICollectionView!

定义单元格的重用标识符

let reuseIdentifier = "actionCell";

为收集视图设置委托和数据源

currentCollectionView.delegate = self
currentCollectionView.dataSource = self

使用重用标识符为收集视图注册单元格

currentCollectionView.register(UINib(nibName: "ActionCollectionViewCell",bundle: nil),forCellWithReuseIdentifier: reuseIdentifier)

UICollectionView委托,数据源和FlowLayoutDelegate的实现

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    return 4;
}

func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

    return 1;
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

    return 1
}

func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {

    return 10
}

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,for: indexPath) as! ActionCollectionViewCell

    cell.vwAction?.layer.cornerRadius = 25.0
    cell.vwAction?.clipsToBounds = true

    if indexPath.row == 0 {

        let imgAction = UIImage(named: "Camera")
        cell.imgAction?.image = imgAction
        cell.vwAction?.backgroundColor = UIColor.black
    } else {

        cell.imgAction?.image = nil
        cell.vwAction?.backgroundColor = self.randomColor()
    }

    return cell
}

生成随机颜色的功能

func randomColor() -> UIColor {

    let red = CGFloat(drand48())
    let green = CGFloat(drand48())
    let blue = CGFloat(drand48())
    return UIColor(red: red,green: green,blue: blue,alpha: 1.0)
}

ActionCollectionViewCell设计enter image description here

本文链接:https://www.f2er.com/3140260.html

大家都在问