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

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

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

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

Q&A

解決済

1回答

560閲覧

Xcode(Swift)で、2つのUICollectionViewを実行させたいのですが上手くいきません。

mimamo

総合スコア44

Swift

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

0グッド

1クリップ

投稿2018/11/25 06:33

前提・実現したいこと

1つ目とは異なるページに2つ目のUICollectionViewを実行したいです。
※Storyboardも操作しています。

発生している問題・エラーメッセージ

1つ目 Invalid redeclaration of 'collectionView(_:numberOfItemsInSection:)' 2つ目 Invalid redeclaration of 'collectionView(_:cellForItemAt:)' 3つ目 Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource' 4つ目 Cannot convert value of type 'IndexPath.Type' to expected argument type 'IndexPath'

該当のソースコード

import UIKit class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { let array:[String] = ["binsen1","binsen2","binsen3","binsen4","binsen5","binsen6","binsen7","binsen8"] let array1:[String] = ["huto1","huto2","huto3","huto4","huto5","huto6","huto7","huto8"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //NUmber of Views その1 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return array.count } //NUmber of Views その2 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return array1.count } //Populate view その1 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell cell.myimageView.image = UIImage(named: array[indexPath.row] + ".jpg" ) return cell } //Populate view その2 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: IndexPath) as! myCell1 cell1.myimageView1.image = UIImage(named: array1[indexPath.row] + ".jpg" ) return cell1 } }

補足情報(FW/ツールのバージョンなど)

https://www.youtube.com/watch?v=nPf5X5z0eA4&t=596s
この動画をみて1つ目は成功したのですが、2つ目はエラーが出てしまいました。
2つ目のコーディングの仕方がわからず苦戦しています。

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

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

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

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

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

guest

回答1

0

ベストアンサー

メソッドは1つにして引数のcollectionViewで判定してください。

Swift

1func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 2{ 3 if collectionView == self.collectionView1 { 4 return array.count 5 }else{ 6 return array1.count 7 } 8}

投稿2018/11/25 10:07

toki_td

総合スコア2850

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

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

mimamo

2018/11/26 04:57

回答ありがとうございます。 collectionView1というのを他では使っていないためエラーが出てしまうのですが、どこにcollectionView1を書き込めばいいのかわかりません。よろしくお願いします。
退会済みユーザー

退会済みユーザー

2018/11/26 07:40 編集

collectionView1書き込まなくていいですよね。 @toki_tdの回答を書き換えてみます。 if collectionView == "array1を表示したいcollectionView" { return array1.count } か if "array1を表示したい" == true { return array1.count } 二つの func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return array.count } ではなくて、一つのメソッドを使って、その中にarrayとarray1を分岐するロジックを入れて下さい。
toki_td

2018/11/26 09:00

んっ?そもそもStoryboardとつながってます? ↓あたりを参考にStoryboard上のCollectionViewとソースをIBOutletでつないで下さい。 https://i-app-tec.com/ios/button.html
mimamo

2018/11/28 05:21

お返事遅くなり申し訳ありません。いろいろいじっていたら実行ができなくなってしまいました。 お二人がおっしゃるように、ひとつのメソッドにすることはわかりました。 myCell.swiftというファイルで import UIKit class myCell: UICollectionViewCell { @IBOutlet weak var myimageView: UIImageView! } myCell1.swiftというファイルで import UIKit class myCell1: UICollectionViewCell { @IBOutlet weak var myimageView1: UIImageView! } としているのですがこれは繋がっていることにならないのでしょうか? @IBOutletの右側には◉も付いています。 追記 以前まではできていた一つ目のcollectionViewの表示もできなくなってしまし、以下のようなエラーが出てしまいました。The collectionView outlet from the ViewController to the UIImageView is invalid. Outlets cannot be connected to repeating content.
mimamo

2018/11/28 14:21

すみません、上のエラー解決できました。 StoryboardとCollectionViewが繋がっていませんでした。繋げてみると2つのCollectionViewを表示させることができました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問