快速更新collectionview,重新加载数据

时间:2020-06-28 13:31:36

标签: ios swift xcode button uicollectionview

我目前有两个collectionview,每个视图都与一个String数组链接,并嵌入有按钮。我要创建它,以便可以在底部的collectionview上选择一个值,然后通过再次点击将其放到顶部的collectionview上的某个位置。每次点击按钮时,我都不确定如何更新收藏夹视图。

import UIKit
class MyButtonCell: UICollectionViewCell{
    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var targetButton: UIButton!
    
    var callback: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
        
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() -> Void {
        contentView.layer.borderWidth = 1
        contentView.layer.borderColor = UIColor.black.cgColor
    }
    
    @IBAction func buttonTapped(_ sender: UIButton) {
        
        callback?()
    }
}

class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let buttonTitles: [String] = [
        "4", "6", "7", "8"
    ]
    
    var targetButtonTitles: [String] = [
        "", "", "", ""
    ]
    
    var current:String = ""
    
    @IBOutlet var collectionView: UICollectionView!
    
    @IBOutlet var targetCollection: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        targetCollection.delegate = self
        targetCollection.dataSource = self
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return buttonTitles.count
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
        let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell

           // set the button title (and any other properties)
        
         
           if collectionView == self.collectionView {
              
               // Setup here your cell
               cell.callback = {
                   print("Button was tapped at \(indexPath)")
                   self.targetButtonTitles[indexPath.item] = self.buttonTitles[indexPath.item]
                   
                   //print(self.targetButtonTitles)
                self.current = self.buttonTitles[indexPath.item]
                print(self.current)
                   
                   // do what you want when the button is tapped
               }

               cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])

               return cell
           } else {

               // Setup here your targetCell
            
            cell.callback = {
                if self.current != ""{
                    self.targetButtonTitles[indexPath.item] = self.current
                    targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
                    

                }
            }
            
            targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])

               return targetCell
           }
        
    }
}

enter image description here

我不确定如何将“返回目标单元格”放入cell.callback或iBaction

1 个答案:

答案 0 :(得分:0)

我建议您通过集合视图的标签来执行逻辑。

override func viewDidLoad() {
    super.viewDidLoad() 
    collectionView.tag = 1
    targetCollection.tag = 2
}

例如,在您的代表中,通过检查哪个集合视图正在请求数据来创建并返回单元格

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView.tag == 1 {
        return buttonTitles.count
     } else if collectionView.tag == 2 {
        return targetButtonTitles.count
    }    
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView.tag == 1 {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
       cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
    .
    .
    .
    return cell
    } else if collectionView.tag == 2 {
       let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
       cell.buttonOne.setTitle(targetButtonTitles[indexPath.item], for: [])

    .
    .
    .
    return cell
    }  
}

在回调中,在两个数组中移动数据,完成后,重新加载两个集合视图。

self.targetButtonTitles[indexPath.item] = self.buttonTitles[indexPath.item]

collectionView.reloadData()
targetCollection.reloadData()