生成するセルが偶数個の場合のみ最後のセルに左右のスペースが反映されません。
どうしてでしょうか?
Swift
1import UIKit 2 3class ChatTopViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 4 5 6 @IBOutlet weak var chatTopCollectionView: UICollectionView! 7 8 // 画像の名前を配列とする 9 let jumpCoverImageArr = ["jump1", "jump2", "jump3", "jump4", "jump5", "jump6", "jump7", "jump8", "jump9", "jump10"] 10 11 let jumpDateArr = ["2016年10月8日 45号", "2016年10月3日 44号", "2016年9月26日 43号", "2016年9月17日 42号", "2016年9月12日 41号", "2016年9月5日 40号", "2016年8月29日 39号", "2016年8月22日 38号", "2016年8月8日 36・37号", "2016年8月1日 35号"] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 self.chatTopCollectionView.register(UINib(nibName: "ChatTopCell", bundle: nil), forCellWithReuseIdentifier: "ChatTopCell") 17 18 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 } 24 25 // collectionViewのセルを返す 26 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 27 28 let chatTopCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChatTopCell", for: indexPath) as! ChatTopCell 29 30 // Tag番号を使ってImageViewのインスタンス生成 31 let chatTopImageView = chatTopCell.contentView.viewWithTag(1) as! UIImageView 32 33 // 画像配列の番号で指定された要素の名前の画像をUIImageとする 34 let cellImage = UIImage(named: jumpCoverImageArr[indexPath.row]) 35 36 // UIImageをUIImageViewのimageとして設定 37 chatTopImageView.image = cellImage 38 39 // Tag番号を使ってLabelのインスタンス生成 40 let dateLabel = chatTopCell.contentView.viewWithTag(2) as! UILabel 41 dateLabel.text = jumpDateArr[indexPath.row] 42 43 return chatTopCell 44 } 45 46 // Screenサイズに応じたセルサイズを返す 47 // UICollectionViewDelegateFlowLayoutの設定が必要 48 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 49 let cellSize:CGFloat = self.view.frame.size.width*0.9/2 50 let jumpImage = UIImage(named: jumpCoverImageArr[indexPath.row]) 51 let cellImageHeight = jumpImage?.size.height 52 let cellImageWidth = jumpImage?.size.width 53 let rateImge = cellImageHeight! / cellImageWidth! 54 55 // 正方形で返すためにwidth,heightを同じにする 56 return CGSize(width: cellSize, height: cellSize * rateImge + 40) 57 58 } 59 60 // セクション数の設定、今回は1つにセット 61 private func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 62 return 1 63 } 64 65 // セルの左右にスペースを空ける 66 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 67 let cellSpace:CGFloat = self.view.frame.size.width*0.025 68 return UIEdgeInsetsMake(0, cellSpace, 0, cellSpace) 69 } 70 71 // collectionViewのセルの数を返す 72 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 73 return 8; 74 } 75 76 //縦の間隔を決定する 77 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 78 return 0 79 } 80 81 // Cell が選択された場合 82 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 83 84 // [indexPath.row] から画像名を探し、UImage を設定 85// let selectedImage = UIImage(named: jumpCoverImageArr[(indexPath as NSIndexPath).row]) 86// let selectedLabel = UILabel(named: jumpDateArr[(indexPath as NSIndexPath).row]) 87// if selectedImage != nil or selectedLabel != nil { 88 // SubViewController へ遷移するために Segue を呼び出す 89 performSegue(withIdentifier: "toChatTitleViewController",sender: nil) 90// } 91 92 } 93 94 override func prepare(for segue: UIStoryboardSegue, sender: Any!) { 95 if (segue.identifier == "toChatTitleViewController") { 96// let subVC: SubViewController = (segue.destination as? SubViewController)! 97 // SubViewController のselectedImgに選択された画像を設定する 98// subVC.selectedImg = selectedImage 99 } 100 } 101 102} 103
あなたの回答
tips
プレビュー