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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

2259閲覧

collectionViewのカスタムレイアウトについての質問 [swift]

kou009

総合スコア16

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2018/05/30 10:18

[今回聞きたいこと]
・numberOfItemsInSection と、numberOfSections の return の値をどうすればいいでしょうか?

 
インスタグラムの類似アプリを作っています。

こちらのサイトのコードを、ほぼそのまま使って、CollectionViewのカスタムレイアウトを実装しています。
https://swiswiswift.com/2017/11/26/custum-collectionview/

今回実装しているcollectionViewのレイアウトはこの画像と同じものです。
イメージ説明

CustomCollectionViewFlowLayoutクラスを、コレクションビューにアタッチしています。
コードはこちらのページのCustomCollectionViewFlowLayoutクラスのコードと全く同じです。

イメージ説明

firebaseから引っ張ってきたデータ(今回の場合だとpostオブジェクトの中に入っている画像url)をコレクションビューに表示させたいです。
データーを一つ取ってくるごとに、collectionViewがreloadData()されるようになっています。

numberOfItemsInSection と、numberOfSections の return の値をどのように変えれば、outofindexなどのエラーを出さずに表示させることができるでしょうか?

Swift

1import UIKit 2import SDWebImage 3import FirebaseDatabase 4 5class DiscoverViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 6 7 @IBOutlet weak var myCollectionView: UICollectionView! 8 var REF_POSTS = Database.database().reference().child("posts") 9 var posts: [Post] = [] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 myCollectionView.delegate = self 14 myCollectionView.dataSource = self 15 16 loadTopPosts() 17 } 18 19 func loadTopPosts() { 20 21 REF_POSTS.queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value, with: { snapshot in 22 let arraySnapshot = (snapshot.children.allObjects as! [DataSnapshot]).reversed() 23 arraySnapshot.forEach({ (child) in 24 if let dict = child.value as? [String: Any] { 25 let post = Post.transformPostPhoto(dict: dict, key: child.key) 26 self.posts.append(post) 27 self.myCollectionView.reloadData() 28 } 29 }) 30 } 31 } 32 33 //Cellが選択された際に呼び出される 34 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 35 print("Section: (indexPath.section)") 36 print("Num: (indexPath.row)") 37 print("Number: (indexPath.section * 6 + indexPath.row)") 38 } 39 40 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 41 42 return 6 //←ここの値を変える 43 44 } 45 //セクションの総数を返す 46 func numberOfSections(in collectionView: UICollectionView) -> Int { 47    return 8 //←ここの値を変える 48 } 49 50 //Cellに値を設定する 51 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 52 53 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CustomCollectionViewCell 54 55 56 57 let photoUrlString = self.posts[indexPath.section * 6 + indexPath.row].imageUrls[0] //←エラー 58 let photoUrl = URL(string: photoUrlString) 59 cell.imageView1.sd_setImage(with: photoUrl) 60 61 62 63 return cell 64 } 65}

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

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

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

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

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

guest

回答1

0

ベストアンサー

表示される総セル数は6*8=48ですが、posts.countが48個未満なのではないでしょうか。

numberOfSectionは (posts.count-1)/6+1
numberOfItemsInSectionは section<posts.count/6 ? 6 : posts.count%6
をそれぞれ返せばいいのではないでしょうか?
動作は確認してないので、ミスがあるかもしれませんが

投稿2018/05/30 11:36

編集2018/05/30 11:46
on0z

総合スコア25

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

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

kou009

2018/05/30 22:35

ありがとうございます。教えていただいたコードで動きました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問