iOS swift:UICollectionview 水平滚动单个单元格不重新加载

问题描述

在我的应用程序 UICollection 中水平滚动。设计了 collection-view cell 两个按钮和一个 UIView。

每个单元格加载两个按钮。当用户单击一个按钮时,应重新加载特定单元格,并且 UIView 将显示在该单元格中,只有其他单元格不显示

这里我附上了collectionview图片

enter image description here

这是我的代码

     @IBOutlet weak var dataCollectionView: UICollectionView!
        var addindexArray:[Int] = []
        var arraySelectedFilterIndex = [IndexPath]()
        
        self.listArr = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
        
        override func viewDidLoad() {
                super.viewDidLoad()
             self.dataCollectionView.reloadData()
        }
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
            return (lvsnListArr.count/2)
        }
    
          func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                
                let cell = dataCollectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier_CollectionView,for: indexPath as IndexPath) as! DataCell
        
                if self.arraySelectedFilterIndex.contains(indexPath) {
        
                    cell.buttonView.isHidden = false
        
                }else{
                   cell.buttonView.isHidden = true
                }
                
                cell.btn1.setTitle(lvsnListArr[2 * indexPath.row],for: .normal)
                cell.btn1.tag = indexPath.row
                cell.btn1.addTarget(self,action: #selector(btnpressed(sender:)),for: .touchUpInside)
        
                cell.btn2.setTitle(lvsnListArr[2 * indexPath.row + 1],for: .normal)
                cell.btn2.tag = indexPath.row
                cell.btn2.addTarget(self,action: #selector(btn2pressed(sender:)),for: .touchUpInside)
        
                return cell
            }
    
        @objc func btnpressed(sender: UIButton) {
            
            self.addindexArray.append(sender.tag)
            
            let hitPoint = sender.convert(CGPoint.zero,to: dataCollectionView)
            if let indexPath = dataCollectionView.indexPathForItem(at: hitPoint) {
                print("indexPath--->",indexPath)
                self.arraySelectedFilterIndex.append(getIndexPath)
                self.dataCollectionView.reloadItems(at: [getIndexPath])
                
            }
        }

  @objc func btn2pressed(sender: UIButton) {
    
    self.addindexArray.append(sender.tag)
    
    let hitPoint = sender.convert(CGPoint.zero,to: dataCollectionView)
    if let indexPath = dataCollectionView.indexPathForItem(at: hitPoint) {
        print("indexPath--->",indexPath)
        self.arraySelectedFilterIndex.append(getIndexPath)
        self.dataCollectionView.reloadItems(at: [getIndexPath])
        
    }
}

我的错误

我点击了单元格 btn1 一个动作 btnpressed 单个单元格过载并显示一个单元格图像,两个图像在集合视图中重叠。

这里附上了我的问题单元格图片

enter image description here

这里我得到了单元格索引路径和索引路径.row,我如何在单元格中验证和修复这个问题。在这一点上挣扎。

Kinldy 帮助解决这个问题。提前致谢。

解决方法

您可以像在按钮操作中一样重新加载单个单元格

self.dataCollectionView.reloadItems(at: [getIndexPath])

您可以在按钮操作中获取单元格,如下所示

if let cell = self.dataCollectionView.cellForItem(at: indexPath) as? DataCell
{
        //Here you can write your view hide show code
        if self.arraySelectedFilterIndex.contains(indexPath) 
        {
            cell.buttonView.isHidden = false
        }
        else
        {
            cell.buttonView.isHidden = true
        }
}