###前提・実現したいこと
[iPhone] UICollectionView で マス目表示
このサイトを元にして作りました。
実現したい機能は、セルを押した回数を格納する配列である result の要素が書き換わったら、その瞬間に、let Label2 = testCell.contentView.viewWithTag(3) as! UILabel の Label2(押された回数を表示するラベル)を書き換えたいです。
###試したこと
@IBAction func selected5(_ sender: Any)関数の中でLabel2を書き換える方法や、関数が呼ばれた際に collectionView.reloadData() を行う方法を調べたのですが、イマイチ見つかりませんでした。
ご教授お願いします。
###該当のソースコード
Swift
1import UIKit 2 3class ViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout { 4 5 let photos: [String] = ["nagi", "toko","saya","yumiko","yuyu","yuka","miki","mai","kurumi","katakuriko"] 6 // セルを押した回数を格納する配列 7 var result:[Int] = Array(repeating: 0, count: 10) 8 // 押されたセル番号を格納する変数 9 var selectedItem = 0 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 } 13 14 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 15 16 // Cell はストーリーボードで設定したセルのID 17 let testCell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 18 19 // Tag番号を使ってImageViewのインスタンス生成 20 let imageView = testCell.contentView.viewWithTag(1) as! UIImageView 21 // 画像配列の番号で指定された要素の名前の画像をUIImageとする 22 let cellImage = UIImage(named: photos[(indexPath).row]) 23 // UIImageをUIImageViewのimageとして設定 24 imageView.image = cellImage 25 26 // Tag番号を使ってLabelのインスタンス生成 27 let Label = testCell.contentView.viewWithTag(2) as! UILabel 28 Label.text = photos[(indexPath).row] 29 let Label2 = testCell.contentView.viewWithTag(3) as! UILabel 30 Label2.text = String(result[(indexPath).row]) 31 32 return testCell 33 } 34 35 36 // Screenサイズに応じたセルサイズを返す 37 // UICollectionViewDelegateFlowLayoutの設定が必要 38 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 39 40 // 横に3つのCellを入れることを想定して、多少のマージンを入れる 41 let cellSize:CGFloat = self.view.frame.width/3-4 42 43 // 正方形で返すためにwidth,heightを同じにする 44 return CGSize(width: cellSize, height: cellSize) 45 } 46 47 func numberOfSections(in collectionView: UICollectionView) -> Int { 48 return 1 49 } 50 51 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 52 // 要素数を入れる、要素以上の数字を入れると表示でエラーとなる 53 return 10; 54 } 55 56 // Cell が選択された場合 57 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 58 59 // [indexPath.row] から画像名を探し、UImage を設定 60 let selectedImage = UIImage(named: photos[(indexPath).row]) 61 // 配列resultにセルを押した回数を格納 62 if selectedImage != nil { 63 print((indexPath).row) 64 result[(indexPath).row] += 1 65 print(result) 66 } 67 // 現在のセルの番号 68 selectedItem = (indexPath).row 69 // セルをリロード 70 collectionView.reloadData() 71 } 72 73 // セルを5回押したとするボタン selectedItemから現在のセル番号を取得している 74 @IBAction func selected5(_ sender: Any) { 75 result[selectedItem] += 5 76 print(result) 77 } 78 79 override func didReceiveMemoryWarning() { 80 super.didReceiveMemoryWarning() 81 // Dispose of any resources that can be recreated. 82 } 83} 84
###補足情報(言語/FW/ツール等のバージョンなど)
Swift4 Xcode9.01
回答2件
あなたの回答
tips
プレビュー