Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

iOS swift: UICollectionview horizontal scroll single cell not reloading

In my application UICollection scroll horizontally. collection-view cell two button and one UIView was designed.

Each cell loaded two button. when user click one button the particular cell should be reload and the UIView will be displayed in that cell only other cell not displayed.

Here i attached the collectionview image: enter image description here

Here my code:

     @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])
        
    }
}

My error:

I clicked cell btn1 one action btnPressed single cell over load and display the next cell image both images are overlap in collectionview.

Here i attached my issue cell image.

enter image description here

Here i got cell indexpath and indexpath.row, how can i validate and fix this issue in cell for row. struggling this point.

Kinldy help to fix this issues. Thanks advance.

question from:https://stackoverflow.com/questions/65839835/ios-swift-uicollectionview-horizontal-scroll-single-cell-not-reloading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can reload single cell like you are doing in button action

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

OR

You can get cell in button action like as below

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
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...