Ios 如何在满足某些条件后立即重新加载UICollectionView单元格
我试图构建一个简单的级别设计,但在满足条件后立即重新加载单元格中的数据时遇到了一些问题。用户可以选择使用辅助游戏货币(stars)解锁他们还没有足够点数的关卡。当他们点击一个锁定的关卡时,如果他们有足够的星星,他们可以选择解锁它 我遇到的问题是,一旦模式被取消,级别不会立即解锁。我必须更改视图,然后返回到级别集合视图,以便单元格重新加载数据 我的故事板: 我的代码如下: 视图控制器Ios 如何在满足某些条件后立即重新加载UICollectionView单元格,ios,swift,xcode,uicollectionview,reloaddata,Ios,Swift,Xcode,Uicollectionview,Reloaddata,我试图构建一个简单的级别设计,但在满足条件后立即重新加载单元格中的数据时遇到了一些问题。用户可以选择使用辅助游戏货币(stars)解锁他们还没有足够点数的关卡。当他们点击一个锁定的关卡时,如果他们有足够的星星,他们可以选择解锁它 我遇到的问题是,一旦模式被取消,级别不会立即解锁。我必须更改视图,然后返回到级别集合视图,以便单元格重新加载数据 我的故事板: 我的代码如下: 视图控制器 import UIKit var stars = 10 var points = 11 var complet
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
}
}
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
}
}
levelCollectionViewCell
import UIKit
class LevelCollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var lockedLevel: UIButton!
@IBOutlet weak var levelName: UILabel!
}
modalViewController
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
}
}
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(模拟在整个游戏中获得点数和星星)
在您共享的代码中,我没有看到对modalViewController的存储引用。但是,处理此问题的最佳方法是将您的
@IBAction func dismissModal(u-sender:Any)
更改为:
@IBAction func back(_ sender: Any)
{
collectionView.reloadData()
dismiss(animated: true, completion: nil)
}
现在,根据VC呈现给您的modalViewController的是哪个/什么,您可能需要创建一个协议来处理重新加载,或者在创建时在您的modal中存储一个引用,这取决于您,但听起来您希望collectionView在用户做出选择后更新其数据/视图,这就是我将如何处理的
您还可以覆盖UIViewController的视图将消失
,以触发重新加载collectionView:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
collectionView.reloadData()
}
在您共享的代码中,我没有看到对modalViewController的存储引用。但是,处理此问题的最佳方法是将您的
@IBAction func dismissModal(u-sender:Any)
更改为:
@IBAction func back(_ sender: Any)
{
collectionView.reloadData()
dismiss(animated: true, completion: nil)
}
现在,根据VC呈现给您的modalViewController的是哪个/什么,您可能需要创建一个协议来处理重新加载,或者在创建时在您的modal中存储一个引用,这取决于您,但听起来您希望collectionView在用户做出选择后更新其数据/视图,这就是我将如何处理的
您还可以覆盖UIViewController的视图将消失
,以触发重新加载collectionView:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
collectionView.reloadData()
}
你的意思是
collectionView.reloadData()
?你的意思是collectionView.reloadData()
?