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

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

新規登録して質問してみよう
ただいま回答率
85.50%
MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

0回答

209閲覧

CollectionViewをスクロールさせる。カスタムセルに値を渡す。

takanori797

総合スコア12

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/12/05 07:50

編集2022/01/12 10:55

前提・実現したいこと

ごめんなさい。独学で初心者です...

ショップのリストを前のViewControllorでtableViewで作り、
下のソースコードですが、collectionviewとcollectionViewCellを作りました。

やりたいことは二つあります。
・なぜかcollectionviewがスクロールできないので、スクロールできるようにしたいです。
・カスタムセルに値を渡したいです。
###環境
swift4.2,Xcode10.1,macOS10.14.1

該当のソースコード

swift

1import UIKit 2import SDWebImage 3import Firebase 4 5class TopViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 6 7 var name_Array = [String]() 8 var title_Array = [String]() 9 var pictureURL_Array = [String]() 10 11 12 @IBOutlet var tableView: UITableView! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 tableView.delegate = self 18 tableView.dataSource = self 19 20 // Do any additional setup after loading the view. 21 } 22 23 override func viewWillAppear(_ animated: Bool) { 24 super.viewWillAppear(animated) 25 26 fetchPost() 27 28 } 29 30 func fetchPost(){ 31 32 //firebaseからデータをとってくる 33 let ref = Database.database().reference() 34 ref.child("post").queryLimited(toFirst: 10).observeSingleEvent(of: .value) { (snap, error) in 35 36 //postsSnap配列にNSDictionary型でデータを入れる 37 let postsSnap = snap.value as? [String: NSDictionary] 38 if postsSnap == nil{ 39 //データなし、まあ呼ばれることはないだろう 40 return 41 } 42 // postsSnapのStringのとこを全て取り出して入れる 43 for (_, list) in postsSnap!{ 44 45 if let name = list["shopName"] as? String, 46 let title = list["title"] as? String, 47 let pictureURL = list["pictureURL"] as? String{ 48 49 self.name_Array.append(name) 50 self.title_Array.append(title) 51 self.pictureURL_Array.append(pictureURL) 52 } 53 } 54 self.tableView.reloadData() 55 } 56 } 57 58 func refleshData(){ 59 //indecator and reload 60 } 61 62 //必須:セルを作る!(各行に表示するセルをかえす) 63 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 64 65 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 66 67 let acsentImage = cell.viewWithTag(1) as! UIImageView 68 let acsentImageURL = URL(string: self.pictureURL_Array[indexPath.row] as String)! 69 acsentImage.sd_setImage(with: acsentImageURL, completed: nil) 70 71 let titleLabel = cell.viewWithTag(2) as! UILabel 72 titleLabel.text = self.title_Array[indexPath.row] 73 74 let nameLabel = cell.viewWithTag(3) as! UILabel 75 nameLabel.text = self.name_Array[indexPath.row] 76 77 return cell 78 } 79 80 //必須:各セクションごとの行数、セルの数 81 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 82 83 return title_Array.count 84 } 85 //セクションの数、今は一個 86 func numberOfSections(in tableView: UITableView) -> Int { 87 return 1 88 } 89 //セルの高さ?? 90 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 91 92 //スクエアにしたいから画面の横幅を高さに指定 93 return UIScreen.main.bounds.size.width 94 } 95 96 //cellをタッチした時の処理 97 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 98 print(self.title_Array[indexPath.row]) 99 print(self.name_Array[indexPath.row]) 100 performSegue(withIdentifier: "TopToDetail", sender: indexPath) 101 } 102 103 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 104 if let detailVC = segue.destination as? DetailViewController, let indexPath = sender as? IndexPath { 105 detailVC.titleLabel = self.title_Array[indexPath.row] 106 detailVC.shopNameLabel = self.name_Array[indexPath.row] 107 } 108 } 109}

swift

1import UIKit 2import SDWebImage 3import Firebase 4import MapKit 5 6class DetailViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{ 7 8 9 @IBOutlet var collectionView: UICollectionView! 10 11 // pass valueds 12 var titleLabel: String = "" 13 var shopNameLabel: String = "" 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 } 19 20 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 21 return 3 22 } 23 24 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 25 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell 26// collectionView.alwaysBounceVertical = true 27 collectionView.frame.size.height = 800 28 29 if indexPath == [0,0]{ 30 cell.frame = CGRect(x: 6, y: 6, width: view.frame.width-12, height: view.frame.width-12) 31 }else if indexPath == [0,1]{ 32 cell.frame = CGRect(x: 6, y: view.frame.width, width: view.frame.width-12, height: 100) 33 }else if indexPath == [0,2]{ 34 cell.frame = CGRect(x: 6, y: view.frame.width + 106 , width: view.frame.width - 12, height: 200) 35 } 36 37 cell.selectUI(indexPath: indexPath) 38 return cell 39 } 40} 41

swift

1import UIKit 2import MapKit 3import Firebase 4 5class CollectionViewCell: UICollectionViewCell { 6 7 let imgScrollView = UIScrollView() 8 let titleLabel = UILabel() 9 let nameLabel = UILabel() 10 let map = MKMapView() 11 12 var cardView: [UIView] = [] 13 var imageURL_Array: [String] = [] 14 15 let ref = Database.database().reference() 16 17 //as e.g, 18 let egURL1 = "https://leadershipayau.com/wp-content/uploads/2018/09/Page-23-1.jpg" 19 let egURL2 = "https://www.xinalaniretreat.com/wp-content/uploads/2016/07/Yoga-1.jpg" 20 21 22 var latitude: CLLocationDegrees = 35.658581 23 var longitude: CLLocationDegrees = 139.745433 24 25 26 func selectUI(indexPath: IndexPath) { 27 if indexPath == [0, 0] { 28 imgScrollView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) 29 30 imageURL_Array.append(egURL1) 31 imageURL_Array.append(egURL2) 32 33 for i in 0 ..< imageURL_Array.count { 34 35 let imageURL = URL(string: imageURL_Array[i] as String)! 36 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: imgScrollView.frame.width, height: imgScrollView.frame.width)) 37 38 imageView.sd_setImage(with: imageURL, completed: nil) 39 imageView.tag = i + 1 40 41 42 self.imgScrollView.addSubview(imageView) 43 44 } 45 46 let topImageURL = URL(string: imageURL_Array[0]) 47 var imgView = UIImageView() 48 imgView.sd_setImage(with: topImageURL, completed: nil) 49 50 var subviews:Array = imgScrollView.subviews 51 52 // 描画開始の x,y 位置 53 var px:CGFloat = 0.0 54 let py:CGFloat = 0.0 55 56 for i in 0 ..< subviews.count { 57 imgView = subviews[i] as! UIImageView 58 if (imgView.isKind(of: UIImageView.self) && imgView.tag > 0){ 59 60 var viewFrame:CGRect = imgView.frame 61 viewFrame.origin = CGPoint(x: px, y: py) 62 imgView.frame = viewFrame 63 64 px += (imgScrollView.frame.width) 65 66 } 67 } 68 // UIScrollViewのコンテンツサイズを画像のtotalサイズに合わせる 69 let nWidth:CGFloat = imgScrollView.frame.width * CGFloat(imageURL_Array.count) 70 imgScrollView.contentSize = CGSize(width: nWidth, height: imgScrollView.frame.width) 71 72 addSubview(imgScrollView) 73 74 75 } else if indexPath == [0, 1] { 76 titleLabel.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height/2) 77 nameLabel.frame = CGRect(x: 0, y: self.frame.height/2, width: self.frame.width, height: self.frame.height/2) 78 titleLabel.textAlignment = NSTextAlignment.center 79 nameLabel.textAlignment = NSTextAlignment.center 80 81 titleLabel.text = "hello" 82 83 addSubview(titleLabel) 84 addSubview(nameLabel) 85 86 87 } else if indexPath == [0, 2] { 88 89 map.frame = CGRect(x: 6, y: 6, width: self.frame.width-6, height: self.frame.width-6) 90 let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 91 map.setCenter(location, animated: true) 92 map.mapType = .standard 93 94 map.region.center = location 95 map.region.span.latitudeDelta = 0.02 96 map.region.span.longitudeDelta = 0.02 97 98 let annotation = MKPointAnnotation() 99 annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude) 100 annotation.title = "東京タワー" 101 map.addAnnotation(annotation) 102 103 addSubview(map) 104 105 } 106 107 } 108} 109 110

試したこと

ここに問題に対して試したことを記載してください。

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

12月6日(木)
皆様リアクション等ありがとうございます!また、丸投げの質問で申し訳ございません。この機能を使えばできる等でも大丈夫です!URLとか投げてくれれば勉強します!勉強不足申し訳ございません!

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

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

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

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

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

t_obara

2018/12/06 07:36

このような動作を期待して、こんなふうに考えたやってみたけれど、こんな風にしか動かなかった。というようなことを記載するとより回答を得られやすいかと。ちなみに、tagを利用している理由はなんですか? Cellは再利用されているので、x行y列が一つのCellに結びついているわけではありません。
takanori797

2018/12/06 08:59

なるほど、xyを設定するのは間違っているのですね。tagを使っている理由はcellに載っているviewはtagで識別する必要があるのかと思っていたためです。他にやり方がありそうですね、、拙い質問内容に関わらす返信頂きまして有難うございます!
t_obara

2018/12/07 08:18

非常に一般的なことなので、CollectionViewControllerについて調べればいくらでも出てくると思いますよ。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問