I have two arrays, I call a separate VC and change the General array to Motivation by clicking the button. When I return to the VC on which UIViewCollection, the displayed array element remains equal to the index of the previous one. How do I start displaying the array from scratch?
VC UIcollectionView
class MainViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var generalQuote = [
Text(text: "general0", name: "quote0"),
Text(text: "general1", name: "quote1"),
Text(text: "general2", name: "quote2"),
Text(text: "general3", name: "quote3"),
Text(text: "general4", name: "quote4"),
]
var motivationQuoute = [
Text(text: "motivation0", name: "quote0"),
Text(text: "motivation1", name: "quote1"),
]
var arrayQuote = [Text]()
enum Theme {
case general, motivation
}
var currentTheme: Theme = .general
var quoutesInTheme = 0
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.register(UINib(nibName: "ProductCell", bundle: nil),forCellWithReuseIdentifier: "ProductCell")
self.collectionView.dataSource = self
self.collectionView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
checkCurrentTheme()
print("Updated")
}
func checkCurrentTheme() {
switch currentTheme {
case .general:
print("Current theme General")
quoutesInTheme = generalQuote.count
arrayQuote = generalQuote
case .motivation:
print("Current theme Motivation")
arrayQuote = motivationQuoute
quoutesInTheme = motivationQuoute.count
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
quoutesInTheme = arrayQuote.count
return quoutesInTheme
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as! ProductCell
let quotes: Text
quotes = arrayQuote[indexPath.item]
cell.setupCell(text: quotes)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let guide = view.safeAreaLayoutGuide
let width = guide.layoutFrame.size.width
let height = guide.layoutFrame.size.height
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
}
VC with Buttons
class choiceThemeQuoteViewController: UIViewController {
@IBOutlet weak var general: UIButton!
@IBOutlet weak var motivation: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func generalButton(_ sender: Any) {
if let presenter = presentingViewController as? MainViewController {
presenter.currentTheme = .general
presenter.collectionView.reloadData()
}
dismiss(animated: true, completion: nil)
}
@IBAction func motivationButton(_ sender: Any) {
if let presenter = presentingViewController as? MainViewController {
presenter.currentTheme = .motivation
presenter.collectionView.reloadData()
}
dismiss(animated: true, completion: nil)
}
}
Do you see the print when you return ?
print("Updated")
If not, this may be due to the type of presentation of choiceThemeQuoteViewController.
If it is automatic did not disappear. Hence viewWillAppear is not called when you get back.
Try to change to full screen: select choiceThemeQuoteViewController in storybaord, go to Attributes inspector.
Another solution (not tested), to keep the same transition: try to call checkCurrentTheme directly
@IBAction func motivationButton(_ sender: Any) {
if let presenter = presentingViewController as? MainViewController {
presenter.currentTheme = .motivation
presenter.collectionView.reloadData()
presenter. checkCurrentTheme() // <<-- ADD THIS
}
dismiss(animated: true, completion: nil)
}