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

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

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

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

Q&A

1回答

719閲覧

swift UICollectionViewCell sectionの最終行だけcellの間隔がずれる

yuki_yuki_yukia

総合スコア10

Swift

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

0グッド

0クリップ

投稿2019/06/12 02:18

swiftを学習しながらアプリを作成しています。

時間割を作成しようとしているのですが、どうしてもsectionの最終行だけ間隔がずれてしまいます。
どのようにすればよろしいでしょうか。

イメージ説明

swift

1import UIKit 2 3class TimeTableViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ 4 5 @IBOutlet var collectionView: UICollectionView! 6 7 let date = ["月","火","水","木","金"] 8 9 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 10 11 if(section == 0){ 12 return 5 + 1 13 }else{ 14 return 30 + 6 15 16 } 17 } 18 19 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 20 21 22 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath) // 表示するセルを登録 23 if(indexPath.section == 0){ 24 cell.backgroundColor = .red 25 //date[indexPath.row] 26 cell.frame.size.height = 15 //曜日の部分のcellの高さ 27 if(indexPath.row % 6 == 0){ 28 cell.frame.size.width = 15 29 } 30 }else{ 31 cell.backgroundColor = .lightGray // セルの色 32 if(indexPath.row % 6 == 0){ 33 cell.frame.size.width = 15 34 } 35 } 36 37 return cell 38 39 } 40 41 //cellのサイズを変更 42 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 43 44 var cellSize: CGSize 45 46 let cellWidth = (collectionView.bounds.width / 5) - 5 47 48 if(indexPath.section == 0){ 49 cellSize = CGSize(width: cellWidth, height: 15) 50 if(indexPath.row % 6 == 0){ 51 cellSize = CGSize(width: 15, height: 15) 52 } 53 }else{ 54 cellSize = CGSize(width: cellWidth, height: cellWidth + 15) 55 if(indexPath.row % 6 == 0){ 56 cellSize = CGSize(width: 15, height: cellWidth + 15) 57 } 58 } 59 60 return cellSize 61 } 62 63 //Cellが選択された際に呼び出される 64 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 65 66 if(indexPath.section == 1 && indexPath.row % 6 != 0){ 67 print("Num: (indexPath.row)") 68 } 69 } 70 71 //コレクションビューのセクション数 今回は2つに分ける 72 func numberOfSections(in collectionView: UICollectionView) -> Int { 73 return 2 74 } 75 76 override func viewDidLoad() { 77 super.viewDidLoad() 78 collectionView.delegate = self 79 collectionView.dataSource = self 80 } 81 82}

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

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

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

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

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

fuzzball

2019/06/13 02:07

>>最終行だけ間隔がずれてしまいます 最終行「以外」がずれているのではないですか? あと、min spacingはどうなってますか?
yuki_yuki_yukia

2019/06/25 22:58

返信が遅くなり申し訳ありません。 はいその通りだと思います。 minspasingは1になっております。 セクションの最後も隙間が自動で広がって欲しいと思っています。
guest

回答1

0

セルのスペースを先に決めておいてセルの大きさを計算するようにしてみました。
こちらでいかがでしょうか?

Swift

1class TimeTableViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ 2 3 @IBOutlet var collectionView: UICollectionView! 4 5 let date = ["月","火","水","木","金"] 6 7 // 新しく追加: Cell間のスペース 8 let cellSpace: CGFloat = 4.0 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 // CollectionView 14 collectionView.delegate = self 15 collectionView.dataSource = self 16 } 17 18 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 19 20 if(section == 0){ 21 return 5 + 1 22 }else{ 23 return 30 + 6 24 } 25 } 26 27 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 28 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath) // 表示するセルを登録 29 30 if(indexPath.section == 0){ 31 cell.backgroundColor = .red 32 } else { 33 cell.backgroundColor = .lightGray // セルの色 34 } 35 36 return cell 37 } 38 39 //cellのサイズを変更 40 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 41 42 var cellSize: CGSize 43 44 // 新しく追加: セルの大きさを計算 45 let squareWidth: CGFloat = 15.0 // 左端のセルのサイズ 46 let cellWidth = ( collectionView.bounds.width - ( cellSpace * 5 ) - squareWidth ) / 5 47 48 if(indexPath.section == 0){ 49 if(indexPath.row % 6 == 0){ 50 cellSize = CGSize(width: squareWidth, height: squareWidth) 51 } else { 52 cellSize = CGSize(width: cellWidth, height: squareWidth) 53 } 54 } else { 55 if(indexPath.row % 6 == 0){ 56 cellSize = CGSize(width: squareWidth, height: cellWidth * 1.2) 57 } else { 58 cellSize = CGSize(width: cellWidth, height: cellWidth * 1.2) 59 } 60 } 61 62 return cellSize 63 } 64 65 //Cellが選択された際に呼び出される 66 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 67 68 if(indexPath.section == 1 && indexPath.row % 6 != 0){ 69 print("Num: (indexPath.row)") 70 } 71 } 72 73 // 新しく追加: Cell間のスペースの最小値 74 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 75 return cellSpace 76 } 77 78 //コレクションビューのセクション数 今回は2つに分ける 79 func numberOfSections(in collectionView: UICollectionView) -> Int { 80 return 2 81 } 82} 83

投稿2019/06/26 15:34

hayabusabusash

総合スコア767

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問