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

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

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

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

Swift

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

Q&A

解決済

1回答

7438閲覧

【Swift】collectionview cellの高さを可変にしたいです

NobumitsuHata

総合スコア141

Xcode

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

Swift

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

0グッド

0クリップ

投稿2016/09/12 05:37

現状、正方形になっているセルのサイズは正方形になっていますが、幅はそのまま画面サイズの半分のままで高さを中身の画像のサイズにしたいです。一番ベストな方法教えて頂けると助かります。

Storyboard

SwifT

1import UIKit 2 3class ChatViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 4 5 // 画像の名前を配列とする 6 let photos = ["jump1", "jump2", "jump3", "jump4", "jump5", "jump6", "jump7", "jump8", "jump9", "jump10"] 7 8 // インスタンス化された直後に発動 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 } 12 13 // collectionViewのセルを返す 14 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 15 16 // dequeueReusableCellWithReuseIdentifier の働きは 17 // 再利用できるセルがあればそれを使う 18 // 再利用できるセルがなければ生成する 19 // Cell はストーリーボードで設定したセルのID 20 let testCell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) 21 22 // Tag番号を使ってImageViewのインスタンス生成 23 let imageView = testCell.contentView.viewWithTag(1) as! UIImageView 24 // 画像配列の番号で指定された要素の名前の画像をUIImageとする 25 let cellImage = UIImage(named: photos[indexPath.row]) 26 // UIImageをUIImageViewのimageとして設定 27 imageView.image = cellImage 28 29 return testCell 30 } 31 32 // Screenサイズに応じたセルサイズを返す 33 // UICollectionViewDelegateFlowLayoutの設定が必要 34 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 35 let cellSize:CGFloat = self.view.frame.size.width/2 36 // 正方形で返すためにwidth,heightを同じにする 37 return CGSizeMake(cellSize, cellSize) 38 } 39 40 //section 数の設定、今回は1つにセット 41 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 42 return 1 43 } 44 45 // collectionViewの要素の数を返す 46 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 47 return 10; 48 } 49 50 override func didReceiveMemoryWarning() { 51 super.didReceiveMemoryWarning() 52 } 53 54 //横の間隔 これで横並びを実装する 55 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 56 return 0 57 } 58 59 // Cell が選択された場合 60 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 61 62 // [indexPath.row] から画像名を探し、UImage を設定 63// selectedImage = UIImage(named: photos[indexPath.row]) 64// if selectedImage != nil { 65 // SubViewController へ遷移するために Segue を呼び出す 66 performSegueWithIdentifier("toChatListViewController",sender: nil) 67// } 68 } 69 70 // Segue 準備 画面遷移時に値を遷移先に渡すならここに書く 71 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 72 if (segue.identifier == "toChatListViewController") { 73// let subVC: SubViewController = (segue.destinationViewController as? SubViewController)! 74 // SubViewController のselectedImgに選択された画像を設定する 75// subVC.selectedImg = selectedImage 76 } 77 } 78 79} 80

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

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

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

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

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

guest

回答1

0

ベストアンサー

Swift

1func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 2 3 let image: UIImage = UIImage(named: photos[indexPath.row]) 4 let width: CGFloat = self.view.frame.size.width / 2 5 let height: CGFloat = (image?.size.height)! 6 7 return CGSizeMake(width, height) 8 9}

cellの中のImageViewAutoLayout設定済みの程だと、
セルサイズを画像の高さに変えてあげればいいはず。

投稿2016/09/12 09:38

編集2016/09/12 09:41
Y_M

総合スコア265

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問