Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 仅更改一个collectionview单元的对象_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 仅更改一个collectionview单元的对象

Ios 仅更改一个collectionview单元的对象,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我想做的是,当我点击单元格中的按钮时,该单元格中的按钮将不可见。问题是,当我点击按钮时,它会变得不可见,但当我滚动收藏视图时,隐藏的按钮会从一个转到另一个。例如,我点击第二个隐藏的图标,但当我滚动时,我看到第七个变为隐藏。每次我滚动隐藏按钮都会改变 这是我写的代码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCe

我想做的是,当我点击单元格中的按钮时,该单元格中的按钮将不可见。问题是,当我点击按钮时,它会变得不可见,但当我滚动收藏视图时,隐藏的按钮会从一个转到另一个。例如,我点击第二个隐藏的图标,但当我滚动时,我看到第七个变为隐藏。每次我滚动隐藏按钮都会改变

这是我写的代码:

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

        cell.lblEsmeMahsul.text = mainCats[indexPath.row]

        cell.imgMahsul.af_setImage(withURL: URL(string : (mainadress + "/Opitures/" + mainPicNumbers[indexPath.row]))!, placeholderImage: UIImage(named: "loadings" ))


        cell.btnKharid.addTarget(self, action: #selector(btnColectionviewCellTapped), for : UIControlEvents.touchUpInside)
        cell.btnKharid.tag = indexPath.row

         cell.btnMosbat.addTarget(self, action: #selector(btnMosbatTapped), for : UIControlEvents.touchUpInside)
        cell.btnMosbat.tag = indexPath.row


        cell.configureCell()
        return cell
    }

@objc func btnColectionviewCellTapped(_ sender:UIButton){
    // let indexPath : IndexPath = self.collectionview1.ind

    print(sender.tag)

    sender.isHidden = true
}

@objc func btnMosbatTapped(_ sender:UIButton){
    let index = IndexPath(item: sender.tag , section: 0)
    let cell = self.collectionviewForushVije.cellForItem(at: index) as? CollectionViewCellKharid3

    cell?.lblTedad.text = "22"
    print(sender.tag)
}

细胞被重新利用。您需要跟踪已点击的单元格,以便在cellForItemAt方法中设置正确的按钮状态

在类中声明属性:

var beenTapped: Set<Int> = []
在cellForItemAt中,您需要:

cell.btnKharid.isHidden = beenTapped.contains(indexPath.item)

您还应该将indexPath.row的使用替换为indexPath.item。行用于表视图。项用于集合视图。

这是UICollectionView或UITableView的一种非常常见的误用。当处理它们时,你应该始终记住一件事:重复使用。在需要时,操作系统将高度重用collection/tableview单元。代码中的问题原因是,假设单元格中一个属性的一次性集是持久性,这是错误的。来自“出列”方法的单元格始终可以是新单元格或现有单元格,因此,任何配置都应应用于单元格,并应再次进行配置。这样想,当一个单元格中的所有视图从集合视图中获取时,它都是脏的,您应该在返回它之前设置您想要的属性,或者在以后设置它的机制。因此,在您的情况下,每次在cellForRow委托中准备单元格时,只需设置isHidden属性。

重写prepareforreuse函数即可设置所有需要的内容。比如按钮的可视性状态。这些单元格是重复使用的,如果你设置不正确,你会得到问题的答案。还有一个问题。我应该如何永久保存它?我写了self.defaults.setbeentaped,forKey:beentaped,但它给了我一个错误,以NSException类型的未捕获异常终止,我在它之前打印了beentaped,它有值。另外,当我使用另一个变量代替Beentaped时,它工作得很好!!我该怎么办?不能在UserDefaults中存储集合。从集合中创建一个数组并存储该数组。
cell.btnKharid.isHidden = beenTapped.contains(indexPath.item)