I am very new to swift. So TLDR I have a collection view which I want to update after I click a button. I have seen various solutions and everyone suggesting to put collectionView.reloadData but I am not understanding where to put this line in my code. Any help will be appreciated.
This is the view controller:
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
}
}
So the way this code works is it deletes the character from the start of the array that user has selected each time user presses the button correctly.
For eg if I choose Array Faded and I press A the Array in the UIControllerView should remove A and update collection view accordingly. Think of it as a tutorial of learning songs on xylophone.
Any help would be appreciated. Thanks.
2
Answers
You can try like this
Don’t call
reloadItems(at: [indexPath])
incellForItemAt:indexPath
– the collection view is in the middle of asking you for the cell there, reload items will make it ask you again.You need to tell the collection view it needs to ask you again when your model charList changes: here:
charList.removeFirst()