GameViewControllerクラスにあるcollectionViewの処理を、別のファイルにあるGameSceneクラスのボタンを押すたびに、cellの数を1増やして、reloadData()の処理をしたいのですが、エラーが出ます。
Swift
1// 2// GameViewController.swift 3// fuga 4// 5 6 7import UIKit 8import SpriteKit 9import GameplayKit 10 11class GameViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { 12 13 @IBOutlet weak var collectionView: UICollectionView! 14 static var count:Int = 3 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 if let view = self.view as! SKView? { 20 // Load the SKScene from 'GameScene.sks' 21 if let scene = SKScene(fileNamed: "GameScene") { 22 // Set the scale mode to scale to fit the window 23 scene.scaleMode = .aspectFill 24 25 // Present the scene 26 view.presentScene(scene) 27 28 29 } 30 31 view.ignoresSiblingOrder = true 32 33 view.showsFPS = true 34 view.showsNodeCount = true 35 } 36 } 37 38 //セルの数 39 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 40 return GameViewController.count 41 } 42 43 //セルのセクション 44 func numberOfSections(in collectionView: UICollectionView) -> Int { 45 return 1 46 } 47 48 //セルにタッチした時の処理 49 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 50 print(indexPath.row) 51 } 52 53 //セルに対する操作 54 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 55 let cell : Custom = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Custom 56 cell.backgroundColor = UIColor.red 57 return cell 58 } 59 60 //reloadするファンクションを作ってみた。 61 func reload() { 62 GameViewController.count += 1 63 collectionView.reloadData() 64 } 65 override var shouldAutorotate: Bool { 66 return true 67 } 68 69 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 70 if UIDevice.current.userInterfaceIdiom == .phone { 71 return .allButUpsideDown 72 } else { 73 return .all 74 } 75 } 76 77 override var prefersStatusBarHidden: Bool { 78 return true 79 } 80 81 82} 83
↑にあるファイルのcollectionViewを↓で処理したいです
Swift
1 2import SpriteKit 3import GameplayKit 4 5 6class GameScene: SKScene, UICollectionViewDelegate, UICollectionViewDataSource { 7 8 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 9 return 1 10 } 11 12 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 13 let cell : Custom = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Custom 14 cell.backgroundColor = UIColor.red 15 return cell 16 } 17 18 19 var reloadButton = SKShapeNode() 20 21 override func didMove(to view: SKView) { 22 23 reloadButton = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 100, height: 100), cornerRadius: 50) 24 reloadButton.fillColor = UIColor.red 25 reloadButton.zPosition = 1 26 self.addChild(reloadButton) 27 28 29 } 30 31 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 32 33 let location = touches.first!.location(in: self) 34 35 let node = atPoint(location) 36 37 if node == reloadButton { 38 GameViewController().reload() //処理の仕方が間違っている? 39 } 40 } 41 42 43} 44
collectionView.delegateをGameSceneクラスにも実装してみたのですがうまく行きませんでした。ご存知の方おられましたら教えてください。よろしくお願いいたします。
>> GameViewController()
これは別の(新規に生成した)インスタンスです。