一旦满足某些条件,如何立即重新加载 UICollectionView 单元格
Posted
技术标签:
【中文标题】一旦满足某些条件,如何立即重新加载 UICollectionView 单元格【英文标题】:How to immediately reload UICollectionView Cells once certain conditions are met 【发布时间】:2017-10-02 16:43:52 【问题描述】:我正在尝试构建一个简单的关卡设计,但在满足条件时立即重新加载单元格中的数据时遇到了一些问题。用户可以选择通过使用辅助游戏货币(星星)来解锁他们还没有足够积分的关卡。当他们点击一个锁定的关卡时,如果他们有足够的星星,他们可以选择将其解锁。
我遇到的问题是,一旦模式被解除,关卡不会立即解锁,我必须更改视图,然后返回关卡集合视图,以便单元格重新加载数据
我的故事板:
我的代码如下: 视图控制器
import UIKit
var stars = 10
var points = 11
var completeLevels:[String] = []
let levels = ["1","2","3","4","5","6","7","8","9"]
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad()
super.viewDidLoad()
override func viewDidAppear(_ animated: Bool)
if points >= 10
completeLevels.append(levels[0])
if points >= 20
completeLevels.append(levels[1])
if points >= 30
completeLevels.append(levels[2])
if points >= 40
completeLevels.append(levels[3])
if points >= 50
completeLevels.append(levels[4])
if points >= 60
completeLevels.append(levels[5])
if points >= 70
completeLevels.append(levels[6])
if points >= 80
completeLevels.append(levels[7])
if points >= 90
completeLevels.append(levels[8])
completeLevels = Array(Set(completeLevels)).sorted()
collectionView.reloadData()
override var prefersStatusBarHidden: Bool
return true
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return levels.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! LevelCollectionViewCell
cell.levelName.text = levels[indexPath.row]
cell.lockedLevel.alpha = 1
for objects in completeLevels
if levels.contains(objects)
if levels.index(of: objects) == indexPath.row
cell.lockedLevel.alpha = 0
return cell
levelCollectionViewCell
import UIKit
class LevelCollectionViewCell: UICollectionViewCell
@IBOutlet weak var lockedLevel: UIButton!
@IBOutlet weak var levelName: UILabel!
模态视图控制器
import UIKit
class modalViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func dismissModal(_ sender: Any)
dismiss(animated: true, completion: nil)
@IBOutlet weak var noStarsLabel: UILabel!
@IBAction func back(_ sender: Any)
dismiss(animated: true, completion: nil)
@IBAction func unlockLevel(_ sender: UIButton)
if stars >= 10
points += 10
stars -= 10
dismiss(animated: true, completion: nil)
else
noStarsLabel.text = "NOT ENOUGH STARS !"
override var prefersStatusBarHidden: Bool
return true
gameViewController(模拟在整个游戏中获得积分和星星)
import UIKit
class gameViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func addStars(_ sender: UIButton)
stars += 10
@IBAction func addPoints(_ sender: UIButton)
points += 10
【问题讨论】:
你的意思是collectionView.reloadData()
?
【参考方案1】:
我在您共享的代码中的任何地方都没有看到对您的 modalViewController 的存储引用。但是,处理此问题的最佳方法是将您的 @IBAction func dismissModal(_ sender: Any)
更改为:
@IBAction func back(_ sender: Any)
collectionView.reloadData()
dismiss(animated: true, completion: nil)
现在,根据哪个/什么 VC 呈现您的 modalViewController,您可能必须创建一个协议来处理重新加载,或者在创建模态时在您的模态中存储一个引用,这取决于您,但这听起来像您想要的在用户做出选择后,collectionView 会更新其数据/视图,这就是我的处理方式。
您还可以覆盖 UIViewController 的 viewWillDisappear
以触发重新加载您的 collectionView:
override func viewWillDisappear(_ animated: Bool)
super.viewWillDisappear(animated)
collectionView.reloadData()
【讨论】:
以上是关于一旦满足某些条件,如何立即重新加载 UICollectionView 单元格的主要内容,如果未能解决你的问题,请参考以下文章
如何根据满足某些条件的现有文件创建和重新排列新的 csv 文件?