生成するセルが偶数個の場合のみ最後のセルに左右のスペースが反映されません。
どうしてでしょうか?
Swift
import UIKit class ChatTopViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { @IBOutlet weak var chatTopCollectionView: UICollectionView! // 画像の名前を配列とする let jumpCoverImageArr = ["jump1", "jump2", "jump3", "jump4", "jump5", "jump6", "jump7", "jump8", "jump9", "jump10"] 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号"] override func viewDidLoad() { super.viewDidLoad() self.chatTopCollectionView.register(UINib(nibName: "ChatTopCell", bundle: nil), forCellWithReuseIdentifier: "ChatTopCell") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // collectionViewのセルを返す func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ let chatTopCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChatTopCell", for: indexPath) as! ChatTopCell // Tag番号を使ってImageViewのインスタンス生成 let chatTopImageView = chatTopCell.contentView.viewWithTag(1) as! UIImageView // 画像配列の番号で指定された要素の名前の画像をUIImageとする let cellImage = UIImage(named: jumpCoverImageArr[indexPath.row]) // UIImageをUIImageViewのimageとして設定 chatTopImageView.image = cellImage // Tag番号を使ってLabelのインスタンス生成 let dateLabel = chatTopCell.contentView.viewWithTag(2) as! UILabel dateLabel.text = jumpDateArr[indexPath.row] return chatTopCell } // Screenサイズに応じたセルサイズを返す // UICollectionViewDelegateFlowLayoutの設定が必要 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let cellSize:CGFloat = self.view.frame.size.width*0.9/2 let jumpImage = UIImage(named: jumpCoverImageArr[indexPath.row]) let cellImageHeight = jumpImage?.size.height let cellImageWidth = jumpImage?.size.width let rateImge = cellImageHeight! / cellImageWidth! // 正方形で返すためにwidth,heightを同じにする return CGSize(width: cellSize, height: cellSize * rateImge + 40) } // セクション数の設定、今回は1つにセット private func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } // セルの左右にスペースを空ける func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { let cellSpace:CGFloat = self.view.frame.size.width*0.025 return UIEdgeInsetsMake(0, cellSpace, 0, cellSpace) } // collectionViewのセルの数を返す func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 8; } //縦の間隔を決定する func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0 } // Cell が選択された場合 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { // [indexPath.row] から画像名を探し、UImage を設定 // let selectedImage = UIImage(named: jumpCoverImageArr[(indexPath as NSIndexPath).row]) // let selectedLabel = UILabel(named: jumpDateArr[(indexPath as NSIndexPath).row]) // if selectedImage != nil or selectedLabel != nil { // SubViewController へ遷移するために Segue を呼び出す performSegue(withIdentifier: "toChatTitleViewController",sender: nil) // } } override func prepare(for segue: UIStoryboardSegue, sender: Any!) { if (segue.identifier == "toChatTitleViewController") { // let subVC: SubViewController = (segue.destination as? SubViewController)! // SubViewController のselectedImgに選択された画像を設定する // subVC.selectedImg = selectedImage } } }
まだ回答がついていません
会員登録して回答してみよう