从collectionView中显示的图像数组中删除图像

时间:2019-07-16 16:20:35

标签: ios swift uicollectionview

我在收藏夹视图中有一个imageView,它是通过从数组中添加图库相机的图像来填充的。我现在可以将图像添加到数组中,我想在轻按按钮时删除图像。此按钮出现在每个collectionView单元上。现在的问题是,当我尝试从阵列中删除该映像时,应用程序崩溃了index out of range或删除了所有单元格。

var imageDataArray: [UIImage] = []

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PicturesCell
        cell.delegate = self
        cell.setup(image: imageDataArray[indexPath.item], indexPath: indexPath)
        return cell
    }

extension PicturesVC: PictureDelegate {
    func removeImage(index: Int) {
        print("Button Tapped g11 \(index)")
        imageDataArray.remove(at: index)
        collectionView.reloadData()
    }
}

我的手机

class PicturesCell: UICollectionViewCell {

    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var propertyImage: UIImageView!
    let disposeBag = DisposeBag()

    weak var delegate: PictureDelegate?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func setup(image: UIImage, indexPath: IndexPath) {
        propertyImage.image = image
        removeImg(indexPath: indexPath)
    }

    func removeImg(indexPath: IndexPath) {
        deleteBtn.rx.tap
            .asDriver().drive(onNext: {
                self.delegate?.removeImage(index: indexPath.item)
            })
            .disposed(by: self.disposeBag)
    }

}

0 个答案:

没有答案