如何在我的UICollectionView中添加额外的静态单元格?

时间:2017-03-20 13:55:28

标签: ios swift uicollectionview

我目前在UICollectionView中显示了一系列照片。我还想要添加的唯一一个额外的静态单元格应该让用户可以打开相机。我使用if-else语句来检测索引。不幸的是,控制台给了我一个索引错误。

准确地说:我希望这个静态单元格位于左上角,然后是我的图像数组。我是否必须添加两个部分,还是应该注册另一个自定义单元格来实现此目的?截至目前,我可以看到我的额外单元格,但是在点击时(索引之外)它不起作用。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count + 1
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoId, for: indexPath) as! PhotosCollectionViewCell

    if indexPath.row == imageArray.count {
        cell.backgroundColor = UIColor.lightGray
        cell.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(tappedCamera)))
    } else {
        cell.imageView.image = imageArray[indexPath.item]
        cell.imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(tappedPhoto)))
    }

    return cell
}

更新了代码(解决方案)

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count + 1
}

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

    if indexPath.row == 0 {
        let cameraCell = collectionView.dequeueReusableCell(withReuseIdentifier: cameraId, for: indexPath) as! CameraCollectionViewCell
        return cameraCell
    }

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tappedPhoto))
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoId, for: indexPath) as! PhotoCollectionViewCell

    cell.imageView.image = imageArray[indexPath.row - 1]
    cell.imageView.addGestureRecognizer(tapGesture)

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        print("Camera")
    } 
}

var startingFrame: CGRect?
var blackBackGroundView: UIView?
var selectedImageFromPicker: UIImage?
var selectedImageCompressed: UIImage?

func tappedPhoto(sender: UIGestureRecognizer) {
    if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {

        let imageView = self.collectionView?.cellForItem(at: indexPath)

        startingFrame = imageView?.superview?.convert((imageView?.frame)!, to: nil)

        let zoomingImageView = UIImageView(frame: startingFrame!)
        zoomingImageView.image = imageArray[indexPath.row - 1]
        zoomingImageView.isUserInteractionEnabled = true
        zoomingImageView.contentMode = .scaleAspectFill
        zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomOut)))

        if let keyWindow = UIApplication.shared.keyWindow {
            blackBackGroundView = UIView(frame: keyWindow.frame)
            blackBackGroundView?.backgroundColor = UIColor.black
            blackBackGroundView?.alpha = 0

            keyWindow.addSubview(blackBackGroundView!)
            keyWindow.addSubview(chooseLabel)
            keyWindow.addSubview(zoomingImageView)

            // Set selected image and compress
            selectedImageFromPicker = imageArray[indexPath.row - 1]
            selectedImageCompressed = selectedImageFromPicker?.resized(withPercentage: 0.1)

            chooseLabel.rightAnchor.constraint(equalTo: keyWindow.rightAnchor, constant: -25).isActive = true
            chooseLabel.bottomAnchor.constraint(equalTo: keyWindow.bottomAnchor, constant: -25).isActive = true

            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                self.blackBackGroundView?.alpha = 1
                self.chooseLabel.alpha = 1

                let height = self.startingFrame!.height / self.startingFrame!.width * keyWindow.frame.width

                zoomingImageView.frame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height)

                zoomingImageView.center = keyWindow.center

                }, completion: {(completed) in
                    // Do nothing
            })
        }
    }
}

1 个答案:

答案 0 :(得分:3)

  

我是否必须添加两个部分,或者我应该注册另一个自定义   细胞来完成这个?

在您的情况下,只需在集合的开头添加一个单元格应该足够公平,不需要对其进行多节。

您的方法应按如下方式实施:

1 - numberOfItemsInSection方法:应该按原样:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count + 1
}

2 - cellForItemAt方法:取决于第一个单元格,是否应该是不同的单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // first row
    if indexPath.row == 0 {
        let cameraCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraCell-ID", for: indexPath)

        // setup the cell...

        return cameraCell
    }

    let defaultCell = collectionView.dequeueReusableCell(withReuseIdentifier: "defaultCell-ID", for: indexPath)

    // setup default cell...

    return defaultCell
}

或者,如果您希望它是同一个单元格,但有一些版本:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-ID", for: indexPath)

    // first row
    if indexPath.row == 0 {
        // setup the cell as cemera cell...
    } else {
        // setup the cell as default cell...
    }

    return cell
}

实际上,没有必要为每个单元格添加UITapGestureRecognizer,您只需要实现collection​View(_:​did​Select​Item​At:​)委托方法:

  

告诉委托指定索引路径中的项目是   地选择。

3 - didSelectItemAt方法:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.row == 0 { // camera cell
        // handle tapping the camera cell
    } else { // default cells
        // handle tapping the default cell

        // don't forget that:
        // getting the first element in 'imageArray' should be imageArray[indexPath.row - 1]
    }
}

希望这会有所帮助。