仅更改 collectionview 中的数据源?
Change only the data source in the collectionview?
我有一个数组
var firstpage = [ Food(name: "rice", image: "rice"),
Food(name: "fish", image: "fish"),
Food(name: "pasta", image: "pasta"),
Food(name: "burger", image: "burger") ]
var secondpage = [ Food(name: "Apple", image: "Apple"),
Food(name: "Orange", image: "Orange")]
var thirdpage = [
Food(name: "ice cream", image: "iceCream"),
Food(name: "Waffel", image: "Waffel")]
lazy var cards = [firstpage, secondpage, thirdpage]
我用相同的代码在不同的视图控制器中显示每一个
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return firstpage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CardCVCell
cell.foodName.text = self.firstpage[indexPath.row].name
cell.foodImage.image = UIImage(named: self.firstpage[indexPath.row].image)!
return cell
}
所以我想重构代码并使其更高效并制作一个带按钮的视图控制器所以当他单击按钮时只有数据源发生变化,首先我创建了一个名为 currentShowingCollectionViewIndex
的变量来更新当用户点击按钮改变数组的索引时
例子 :
第一次进入页面 currentShowingCollectionViewIndex = 0 所以它会显示 'firstpage'
当用户再次单击该按钮时,它将更新为 1 并显示 'Secondpage'
@IBAction func pageNextButton(_ sender: UIButton!) {
currentShowingCollectionViewIndex += 1
if currentShowingCollectionViewIndex == 1 {
foodArray = cards[1] //foodArray is only an empty array that I cope to it.
}
if currentShowingCollectionViewIndex == 2 {
foodArray = cards[2]
}
}
但是代码不工作并且数据没有改变(注意:我尝试打印 currentShowingCollectionViewIndex
值并且它正在更新)但我现在不知道为什么数据没有改变?请任何帮助
感谢您的宝贵时间!
您应该在设置新数据后重新加载 collectionView。
@IBAction func pageNextButton(_ sender: UIButton!) {
currentShowingCollectionViewIndex += 1
if currentShowingCollectionViewIndex == 1 {
foodArray = cards[1] //foodArray is only an empty array that I cope to it.
self.collectionView.reloadData()
}
if currentShowingCollectionViewIndex == 2 {
foodArray = cards[2]
self.collectionView.reloadData()
}
}
我有一个数组
var firstpage = [ Food(name: "rice", image: "rice"),
Food(name: "fish", image: "fish"),
Food(name: "pasta", image: "pasta"),
Food(name: "burger", image: "burger") ]
var secondpage = [ Food(name: "Apple", image: "Apple"),
Food(name: "Orange", image: "Orange")]
var thirdpage = [
Food(name: "ice cream", image: "iceCream"),
Food(name: "Waffel", image: "Waffel")]
lazy var cards = [firstpage, secondpage, thirdpage]
我用相同的代码在不同的视图控制器中显示每一个
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return firstpage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CardCVCell
cell.foodName.text = self.firstpage[indexPath.row].name
cell.foodImage.image = UIImage(named: self.firstpage[indexPath.row].image)!
return cell
}
所以我想重构代码并使其更高效并制作一个带按钮的视图控制器所以当他单击按钮时只有数据源发生变化,首先我创建了一个名为 currentShowingCollectionViewIndex
的变量来更新当用户点击按钮改变数组的索引时
例子 :
第一次进入页面 currentShowingCollectionViewIndex = 0 所以它会显示 'firstpage'
当用户再次单击该按钮时,它将更新为 1 并显示 'Secondpage'
@IBAction func pageNextButton(_ sender: UIButton!) {
currentShowingCollectionViewIndex += 1
if currentShowingCollectionViewIndex == 1 {
foodArray = cards[1] //foodArray is only an empty array that I cope to it.
}
if currentShowingCollectionViewIndex == 2 {
foodArray = cards[2]
}
}
但是代码不工作并且数据没有改变(注意:我尝试打印 currentShowingCollectionViewIndex
值并且它正在更新)但我现在不知道为什么数据没有改变?请任何帮助
感谢您的宝贵时间!
您应该在设置新数据后重新加载 collectionView。
@IBAction func pageNextButton(_ sender: UIButton!) {
currentShowingCollectionViewIndex += 1
if currentShowingCollectionViewIndex == 1 {
foodArray = cards[1] //foodArray is only an empty array that I cope to it.
self.collectionView.reloadData()
}
if currentShowingCollectionViewIndex == 2 {
foodArray = cards[2]
self.collectionView.reloadData()
}
}