我正在尝试构建一个简单的关卡设计,但是在遇到条件时我很难立即重新加载单元格中的数据。用户可以通过使用二级游戏币(星星)来选择解锁他们没有足够积分的等级。当他们点击锁定级别时,如果他们有足够的星星,他们可以选择解锁它。
我遇到的问题是,一旦模态被解除,级别不会立即解锁我必须更改视图然后返回级别集合视图以便单元格重新加载数据
我的代码如下: 的ViewController
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!
}
modalViewController
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
}
}
答案 0 :(得分:0)
我没有在您共享的代码中的任何位置看到对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()
}