質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1119閲覧

swift collectionViewの特定のセルに内容を入れる

tsuji71

総合スコア17

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/07/30 01:55

実現したいこと

collectionViewの特定のセルに文字を入れたいと考えています
以下のコードのようにセルの数を36こに固定し、CellのindexPathを取得した上でif文を使って条件分岐させようとしています

発生した問題

初めは問題なくIndexNumが4のセル(indexPathが[0,3]のセル)のみに文字が表示できているのですが、viewWillApperが呼び出され、collectionView.reloadData()が実行されると、一つづつ文字が表示されるセルが増えていってしまいます

試したこと

デバックを行った結果、if indexNum == 4 { classNameLabel.text = "hi" }
の中身はindexNumが4の時に一度しか呼び出されていないことは確認できました
また、この中に例えばセルの色を変えるようなコードを書いても同様の問題が起きるのでclassNameLabelの問題ではないと思われます

ご不明点ありましたらご連絡ください よろしくお願いいたします

swift

1 @IBOutlet weak var collectionView: UICollectionView! 2 3 // Realm 4 let realm = try! Realm() 5 var task = Task() 6 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 // Do any additional setup after loading the view. 12 collectionView.delegate = self 13 collectionView.dataSource = self 14 15 // CollectionViewのレイアウト 16 let layout = UICollectionViewFlowLayout() 17 layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 18 layout.minimumInteritemSpacing = 1 19 layout.minimumLineSpacing = 1 20 collectionView.collectionViewLayout = layout 21 22 collectionView.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1) 23 24 25 } 26 27 override func viewWillAppear(_ animated: Bool) { 28 super.viewWillAppear(true) 29 collectionView.reloadData() 30 31 } 32 33 34 35 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 36 return 36 37 } 38 39 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 40 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 41 42 // Tag番号 43 let classNameLabel = cell.contentView.viewWithTag(1) as! UILabel 44 UILabel 45 46 let indexNum = indexPath.row + 1 47 48 49 if indexNum == 4 { 50 classNameLabel.text = "hi" 51 52 } 53 54 55 cell.backgroundColor = UIColor.white 56 return cell 57 } 58 59 // セルのレイアウトを設定 60 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 61 62 let width = self.collectionView.layer.bounds.width 63 let height = self.collectionView.layer.bounds.height 64 65 let widthcellSize : CGFloat = width / 6 - 1 66 let heightcellSize : CGFloat = height / 6 - 1 67 return CGSize(width: widthcellSize, height: heightcellSize) 68 } 69 70 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 71 72 let indexNum:Int = indexPath[1] + 1 73 74 let taskArray = try! Realm().objects(Task.self).filter("cellPoint == (indexNum)") 75 76 if taskArray.count != 0 { 77 performSegue(withIdentifier: "toDetailClass", sender: nil) 78 79 } else { 80 performSegue(withIdentifier: "toChoice", sender: nil) 81 } 82 83 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

セルが再利用されていますので、4以外のときはクリアして下さい。

swift

1if indexNum == 4 { 2 classNameLabel.text = "hi" 3} else { 4 classNameLabel.text = "" 5}

swift

1classNameLabel.text = indexNum == 4 ? "hi" : ""

投稿2020/07/30 03:44

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tsuji71

2020/07/30 04:18

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問