実現したいこと
コレクションビューを使ったアプリを作っています。
このコレクションビューの中に テーブルビュー
を入れたいです。
(テーブルビューにはカスタムセルを使いたい)
前提
コレクションビュー初心者です。(初めてです)
発生している問題・エラーメッセージ
テーブルビューの接続ができません。
該当のソースコード
Swift
1 2import UIKit 3 4class TestCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDelegate, UITableViewDataSource { 5 6 // ================================================================================ 7 // モデル: 8 // ================================================================================ 9 struct StudentInfo: Codable, Equatable, Comparable { 10 var name: String 11 var secondName: String 12 var age: Int 13 14 // ソートの為:型を統一する 15 static func == (lhs: StudentInfo, rhs: StudentInfo) -> Bool { 16 return lhs.name == rhs.name 17 } 18 19 // ソートの為:ソートの定義を決める 20 // ここではアルファベット順とする 21 static func < (lhs: StudentInfo, rhs: StudentInfo) -> Bool { 22 return lhs.name < rhs.name 23 } 24 } 25 26 27 // 変数 28 var student_display_array: [StudentInfo] = [] 29 30 31 // segue 32 var receiveStudentName: String = "" 33 34 35 override func viewDidLoad() { 36 super.viewDidLoad() 37 38 // APIから情報取得 39 let StudentInfos = StudentInfo(name: name, secondName: secondName, age: age) 40 41 // 表示用の配列に追加 42 self.student_display_array.append(StudentInfos) 43 44 let searchStudentName = self.receiveStudentName 45 let resultStudentInfos = student_display_array.filter { 46 $0.name.contains(searchStudentName) 47 } 48 49 // ここでテーブルビューに表示する用の構造体に追加する 50 self.student_display_array = resultStudentInfos 51 52 print("----------------------------") 53 print("resultStudentInfosは") 54 print(resultStudentInfos) 55 print("----------------------------") 56 57 } 58 } catch { 59 print("-----------------------") 60 print(error.localizedDescription) 61 print("-----------------------") 62 } 63 } 64 65 // ========================================= 66 // カスタムセル登録 67 // ========================================= 68 tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell") 69 70 } 71 72 73 // MARK: - CollectionView 74 // 数 75 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 76 return 10 77 } 78 // 中身 79 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 80 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 81 cell.backgroundColor = .blue 82 return cell 83 } 84 85 // サイズ 86 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 87 return CGSize(width:self.view.frame.width / 2, height:200) 88 } 89 90 // MARK: - UITableView 91 // 数 92 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 93 return student_display_array.count 94 } 95 96 // 中身 97 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 98 99 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell 100 101 cell.id.text = "\(indexPath.row+1)" 102 103 cell.studentName.text = student_display_array[indexPath.row].name 104 cell.studentSecondName.text = student_display_array[indexPath.row].secondeName 105 106 return cell 107 } 108}
試したこと
・テーブルビューのカスタムセル登録をすると Reference to member 'register' cannot be resolved without a contextual type
・テーブルビューを接続すると error: Illegal Configuration: The tableView outlet from the TestCollectionViewController to the UITableView is invalid. Outlets cannot be connected to repeating content.
2つめのエラー内容をググると https://easyramble.com/outlet-error-in-repeating-content.html のブログがヒットしますが少し内容が違ってあまり参考になりませんでした。
聞きたいこと
・コレクションビューの中にテーブルビューが設置できるかどうか
・設置できる場合はなぜ上記のエラーが出るのか知りたいです。
補足情報(FW/ツールのバージョンなど
コレクションビューが初めてなのでお手柔らかにお願いいたします。






回答1件
あなたの回答
tips
プレビュー