現状、正方形になっているセルのサイズは正方形になっていますが、幅はそのまま画面サイズの半分のままで高さを中身の画像のサイズにしたいです。一番ベストな方法教えて頂けると助かります。
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。