单击单元格内的按钮时从 UICollectionView 中删除单元格

Deleting a cell from UICollectionView when clicking on a button inside the cell

所以,我有一个使用 UITextField 收集用户数据的表单。用户最多可以添加 10 个表单。所以我想到将表单创建到 UICollectionView 单元格中。

我的问题是在此表单中有一个删除按钮,如果不再需要该表单,可以将其删除。但这只适用于第一次我会得到一个错误

Fatal error: Index out of range

我很清楚错误的含义,但我不知道如何跟踪我要专门删除的行。

cell.deleteBtn.rx.controlEvent(.touchDown).subscribe(onNext: {_ in
           
            self.data = (self.viewModel?.form.value)!
            self.data.remove(at: row)
            self.viewModel?.form(self.data)
            self.contentViewHeightConstraints.constant -= CGFloat(779)
            

        }).disposed(by: self.disposeBag)

这就是我删除表格的方式。 (我也在使用 RxSwift,这是我能想到的用数组删除的最简单方法)。

我对 Swift 开发还很陌生,所以请原谅我编写的任何错误代码。 请指导我完成这个。

更新:

所以我把函数改成了这个:

cell.deleteBtn.rx.controlEvent(.touchDown).subscribe(onNext: {_ in
           
            self.data = (self.viewModel?.form.value)!
            self.data.remove(at: row)
            self.viewModel?.form(self.data)
            self.contentViewHeightConstraints.constant -= CGFloat(779)
            // I can't use index.row so I used row
            let indexPath = IndexPath(row: row, section: 0)
            
            self.collectionView.performBatchUpdates({
                self.collectionView.deleteItems(at: [indexPath])
            }){(finished) in
                self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
                
            }

        }).disposed(by: self.disposeBag)

现在 bam 收到此错误:

attempt to delete item 1 from section 0 which only contains 1 items before the update

数据源实现:

self.viewModel!.form.asObservable().bind(to: self.formCV!.rx.items){
        tv,row,item in
            let cell = tv.dequeueReusableCell(withReuseIdentifier: "AddFormCell", for: IndexPath.init(row: row, section: 0)) as! AddFormCell
        
        cell.prepareForReuse()
        cell.initCellView()
        cell.iniStatesList()
        
        cell.formCountLbl.text! += " " + String(row + 1)
    
        if self.viewModel?.form.value.count ?? 0 > 1 {
            cell.deleteBtn.isHidden = false
        }else{
            cell.deleteBtn.isHidden = true
        }

添加新表格是这样的:

@IBAction func addShop(){
    var arr = viewModel?.shops.value

    if(arr?.count ?? 0 < 4) {
        arr?.append(formViewModel(shopName: "", shopAddress: "", shopState: "", shopCity: "", shopPostCode: ""))
        
        viewModel?.form.accept(arr ?? [formViewModel(shopName: "", shopAddress: "", shopState: "", shopCity: "", shopPostCode: "")])
        
        self.contentViewHeightConstraints.constant += CGFloat(779)
    }else{
        self.openError()
        
    }

self.data 数组是一个全局数组,定义为简单地从 ViewModel 中删除表单单元格

小区配置:

func configCollectionView(){
    self.collectionView.register(UINib(nibName: addFormCell.identifier, bundle: .main), forCellWithReuseIdentifier: addFormCell.identifier)

    self.shopsCV.delegate = self
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let size = Int((collectionView.bounds.width) / CGFloat(numberOfItemsPerRow))
    
    return CGSize(width: size, height: 779)
    
}

每行项目数 = 1

所以我通过这样做设法解决了这个问题:

cell.deleteBtn.rx.controlEvent(.touchDown).subscribe(onNext: {_ in
           
            
            let point = cell.deleteBtn.convert(CGPoint.zero, to: self.shopsCV)
            
            guard let indexPath = self.shopsCV.indexPathForItem(at: point)
            else{return}
            
            
            self.data = (self.viewModel?.shops.value)!
            self.data.remove(at: indexPath.row)
            self.viewModel?.shops.accept(self.data)
            
            self.shopsCV.performBatchUpdates({
            }){(finished) in
                
                self.shopsCV.reloadItems(at: self.shopsCV.indexPathsForVisibleItems)
                
            }

        }).disposed(by: cell.disposeBag)
       
            return cell

我不得不在单元格中使用 disposeBag 以防止它点击删除按钮两次或更多次。 如果单元格是随机的而不是有序的,这很好用。