前提・実現したいこと
CollectionViewを使ってアプリをつくっています。
発生している問題・エラーメッセージ
didselectItemAtのところでデバックがかえってきません。
print(indexPath.row)のところです。
Swift
1import UIKit 2 3class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ 4 5 private let cellId = "CollectionViewCell" 6 private let itemsPerRow: CGFloat = 4 7 private let sectionInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0) 8 9 let W = [ 10 "100W", "200W", "300W", "400W", "500W", "600W", "700W", "800W", "900W", "1000W", "1100W", "1200W" 11 ] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view. 16 17 } 18 19 20 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 21 12 22 } 23 24 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 25 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CollectionViewCell 26 cell.backgroundColor = UIColor.lightGray 27 cell.layer.cornerRadius = 20 28 29 return cell 30 } 31 32 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 33 print(indexPath.row) 34 } 35 func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { 36 let cell = collectionView.cellForItem(at: indexPath)! 37 cell.backgroundColor = .black 38 } 39 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 40 41 let paddingSpace = sectionInsets.left * (itemsPerRow + 1) 42 let availableWidth = view.frame.width - paddingSpace 43 let widthPerItem = availableWidth / itemsPerRow 44 return CGSize(width: widthPerItem , height: widthPerItem) 45 } 46 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 47 return sectionInsets 48 } 49 50 // セルの行間の設定 51 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 52 return 10.0 53 } 54 55} 56
試したこと
調べてみましたが、解決しませんでした。
回答1件
あなたの回答
tips
プレビュー