UI通过单击按钮更改数组后,集合视图未重新加载(按钮在 UI 集合视图之外)

UICollection View not reloading after changing the Array from a button click (button is outside UI Collection view)

我对 swift 很陌生。所以 TLDR 我有一个集合视图,我想在单击按钮后更新它。我见过各种解决方案,每个人都建议放置 collectionView.reloadData,但我不明白将这一行放在我的代码中的什么位置。任何帮助将不胜感激。 这是视图控制器:

class myViewController: UIViewController {
    @IBOutlet weak var myCollectionView: UICollectionView!
    
    var charList: Array<String> = []
    
    var data: String = ""
    
    var songTiles = [
        "teri meri": ["D", "A", "G", "A", "F", "G", "A", "F", "G", "A", "G", "F", "E", "D", "E", "D", "C", "D", "E", "F", "E", "F", "E", "G", "A", "G", "F", "E", "D", "D"],
        "faded": ["B", "A", "B", "A"],
        "darmiyan": ["A", "B", "B", "A"],
        "hangover": ["B", "A", "B", "A"],
        "dna": ["A", "A", "A", "A"],
        "fire": ["A", "B", "B", "A"],
        "springday": ["C", "A", "A", "A"],
        "gotcha": ["C", "A", "A", "A"],
        "aurora": ["C", "A", "A", "A"],
        "fadedremix": ["A", "B", "A", "B"]
    ]
    
    @IBOutlet weak var chosenSong: UILabel!


    @IBAction func keyNote(_ sender: UIButton) {
        if (sender.currentTitle == charList[0]) {
            charList.removeFirst()
            print("Correct!")
            playSound(sound: sender.currentTitle!)
        }
    }
    
    func playSound(sound: String) {
        guard let url = Bundle.main.url(forResource: sound, withExtension: "wav") else {
            return

        }
        do {
                    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
                    try AVAudioSession.sharedInstance().setActive(true)
        
                    player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
        
                    guard let player = player else { return }
        
                    player.play()
        
                } catch let error {
                    print(error.localizedDescription)
                }
    }
    
    var player: AVAudioPlayer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        charList = songTiles[data]!
        chosenSong.text = data

        // Do any additional setup after loading the view.
    }

    
}
 

extension myViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return charList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        myCollectionView.reloadItems(at: [indexPath])

        let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCollectionViewCell
        cell.myImage.image = UIImage(named: charList[indexPath.row])
        myCollectionView.reloadItems(at: [indexPath])

        
        return cell
    }
    
    
}

所以这段代码的工作方式是,每次用户正确按下按钮时,它都会从用户选择的数组开头删除字符。 例如,如果我选择 Array Faded 并按 A,UIControllerView 中的 Array 应该删除 A 并相应地更新集合视图。将其视为木琴学习歌曲的教程。 任何帮助,将不胜感激。谢谢

你可以这样试试

@IBAction func keyNote(_ sender: UIButton) {
        if (sender.currentTitle == charList[0]) {
            charList.removeFirst()
            print("Correct!")
            
            let theIndex = IndexPath(row: 0, section: 0)   
            myCollectionView.deleteItems(at: [theIndex])
            playSound(sound: sender.currentTitle!)
        }
    }

不要在 cellForItemAt:indexPath 中调用 reloadItems(at: [indexPath]) - 集合视图正在询问您那里的单元格,重新加载项目会再次询问您。

您需要告诉集合视图它需要在您的模型 charList 更改时再次询问您:此处:charList.removeFirst()