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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

2618閲覧

SwiftでTableViewのCell内の画像をif分岐で表示、非表示させているけどうまくいかない

uemuratt

総合スコア13

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/08/13 16:15

###前提・実現したいこと
TableViewをスクロールしても、最初表示されていた画像が消えないように。
(cellの再描画が原因(?))

###該当のソースコード

Swift

1func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 2 let cell = tableView.dequeueReusableCellWithIdentifier("MovieList", forIndexPath: indexPath) as! MovieListTableViewCell 3 4 // cell内の映画タイトルを設定 5 switch indexPath.section { 6 case 0: cell.movieListText?.text = movieListModel.movieListMonth1[indexPath.row]["title"]! as! String 7 case 1: cell.movieListText?.text = movieListModel.movieListMonth2[indexPath.row]["title"]! as! String 8 case 2: cell.movieListText?.text = movieListModel.movieListMonth3[indexPath.row]["title"]! as! String 9 case 3: cell.movieListText?.text = movieListModel.movieListMonth4[indexPath.row]["title"]! as! String 10 case 4: cell.movieListText?.text = movieListModel.movieListMonth5[indexPath.row]["title"]! as! String 11 case 5: cell.movieListText?.text = movieListModel.movieListMonth6[indexPath.row]["title"]! as! String 12 case 6: cell.movieListText?.text = movieListModel.movieListMonth7[indexPath.row]["title"]! as! String 13 case 7: cell.movieListText?.text = movieListModel.movieListMonth8[indexPath.row]["title"]! as! String 14 case 8: cell.movieListText?.text = movieListModel.movieListMonth9[indexPath.row]["title"]! as! String 15 case 9: cell.movieListText?.text = movieListModel.movieListMonth10[indexPath.row]["title"]! as! String 16 case 10: cell.movieListText?.text = movieListModel.movieListMonth11[indexPath.row]["title"]! as! String 17 case 11: cell.movieListText?.text = movieListModel.movieListMonth12[indexPath.row]["title"]! as! String 18 default: break 19 } 20 21 // Theater or Homeアイコンの表示/非表示 22 if movieListModel.movieListMonth1[indexPath.row]["watched_place"] as? String == "theater" { 23 cell.iconTheaterOrHome?.image = UIImage(named: "icon_theater") 24 } else if movieListModel.movieListMonth1[indexPath.row]["watched_place"] as? String == "home" { 25 cell.iconTheaterOrHome?.image = UIImage(named: "icon_house") 26 } else { 27 cell.iconTheaterOrHome.hidden = true 28 } 29 30 // もう1度見たいアイコンの表示/非表示 31 if movieListModel.movieListMonth1[indexPath.row]["once_again"] as? String == "yes" { 32 cell.iconHeart.image = UIImage(named: "icon_heart") 33 } else if movieListModel.movieListMonth1[indexPath.row]["once_again"] as? String == "no" { 34 cell.iconHeart.hidden = true 35 } else { 36 cell.iconHeart.hidden = true 37 } 38 39 // separatorの左の空きをなくす 40 cell.preservesSuperviewLayoutMargins = false 41 cell.separatorInset = UIEdgeInsetsZero 42 cell.layoutMargins = UIEdgeInsetsZero 43 44 return cell 45 }

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

コード内のコメント「Theater or Homeアイコンの表示/非表示」「もう1度見たいアイコンの表示/非表示」でimageView内の画像を表示させたり、非表示にさせたりしているのですが、TableViewをスクロールして、また上まで戻ると、最初表示されていたimageView内の画像が消えてしまいます。おそらくCellの再描画が原因ではないかと思っているのですが、どうやって解決すればよろしいでしょうか。

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

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

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

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

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

guest

回答1

0

ベストアンサー

セルを生成した直後にまず表示をfalseにしてから始めたらどのようになりますか?

swift

1 2let cell = tableView.dequeueReusableCellWithIdentifier("MovieList", forIndexPath: indexPath) as! MovieListTableViewCell 3 4cell.iconTheaterOrHome.hidden = false 5cell.iconHeart.hidden = false

投稿2016/08/14 01:57

_Kentarou

総合スコア8490

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

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

uemuratt

2016/08/14 05:18

ありがとうございます!すごい単純なことだったんですね。 解決しました^^
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問