質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

1425閲覧

collectionViewを、違うクラスからreloadData()で更新したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/08/07 02:16

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クラスにも実装してみたのですがうまく行きませんでした。ご存知の方おられましたら教えてください。よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2019/08/07 02:30 編集

>> GameViewController() これは別の(新規に生成した)インスタンスです。
guest

回答1

0

GameSceneに、CollectionViewもしくはGameViewControllerのインスタンスを渡して下さい。

投稿2019/08/07 02:30

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問