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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1402閲覧

テーブルビュー2段プロジェクト⇒09 sectionつきcellの選択による表示方法その3 (第2画面にもアイコンイメージを追加)

Tomzy

総合スコア104

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2016/09/23 11:02

編集2016/09/27 05:07

###質問の概要

第1画面より第2画面に遷移して表示していますが、第2画面の行頭に動画またはテキストのアイコンイメージを追加したいのが今回の意図です。

if文で第1画面(View Controller)のsection選択により、
let imgVideo = "Video_b.png"
let imgText = "Book_b.png"
を読み込ませるべく、TableView02にコードを追加してCustomCell.xibにImageを追加したのですが、エラーがでて先に進みません。
助言をお願いします。

現在の第1画面のスクリーンショット
イメージ説明

現在の第2画面のスクリーンショット(ここには行頭にアイコンイメージがありません。第1画面の行頭と同じようなアイコンイメージを追加したいのが意図です。ただし、画像の色は青に統一しますので、動画かテキストのアイコン2種類のみです。
イメージ説明

###書き込んだコード

// TableView02.swift //省略 import UIKit class TableView02: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var tableView02: UITableView! var selectedIndexPathInViewController: NSIndexPath! // section毎の画像配列 let imgVideo = "Video_b.png" let imgText = "Book_b.png" let texts00 = ["01.電話番号で発信(動画)", "02「連絡先」の登録方法(動画)", //省略 var texts : [[String]]! var subtexts : [[String]]! var textsURL : [[String]]! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. texts = [texts00,texts01,texts02,texts03,texts04,texts05,texts06] subtexts = [subtexts00,subtexts01,subtexts02,subtexts03,subtexts04,subtexts05,subtexts06] //省略 tableView02.delegate = self tableView02.dataSource = self let nib = UINib(nibName: "CustomCell", bundle: nil) tableView02.registerNib(nib, forCellReuseIdentifier: "Cell") } //省略 //ここからは第2画面の行数を規定する //ここからは、第2画面で行を表示→ 08の質問に対して、Take One さんから回答を頂き修正し成功。 func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { if selectedIndexPathInViewController.section == 1 { return texts[selectedIndexPathInViewController.row].count } if selectedIndexPathInViewController.section == 2 { return texts[selectedIndexPathInViewController.row+3].count } return 0 } //ここからが問題のところ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //イメージを冒頭に添付 if selectedIndexPathInViewController.section == 1 { // Tag番号 1 で UIImageView インスタンスの生成 let imageView = image1.viewWithTag(1) as! UIImageView //エラーのでるところ imageView.image = imgVideo } if selectedIndexPathInViewController.section == 2 { // Tag番号 1 で UIImageView インスタンスの生成 let imageView = image1.viewWithTag(1) as! UIImageView //エラーのでるところ imageView.image = imgText } else{ } //テキスト表示部分→ 成功しています。 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell if selectedIndexPathInViewController.section == 1 { cell.label1.text = texts[selectedIndexPathInViewController.row][indexPath.row] cell.label2.text = subtexts[selectedIndexPathInViewController.row][indexPath.row] } if selectedIndexPathInViewController.section == 2 { cell.label1.text = texts[selectedIndexPathInViewController.row+3][indexPath.row] cell.label2.text = subtexts[selectedIndexPathInViewController.row+3][indexPath.row] return cell } else{ } return cell }
// CustomCell.swift //省略 import UIKit class CustomCell: UITableViewCell { override func awakeFromNib() { super.awakeFromNib() // Initialization code } @IBOutlet weak var image1: UIImageView! //今回追加 @IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } }

###CustomCell.xibにImageを追加したスクリーンショット

イメージ説明

###発生したエラー

イメージ説明
イメージ説明

###発生したエラーに対する考察
image1を宣言していないという意味と解釈して
TableView02上で冒頭に
let image1 = UIImage.self

と追記しましたが、今度は下記のエラーが発生しました。
イメージ説明

###質問

まだ、コードの意味が完全に理解をしていませんので、これ以上進めませんでした。
助言をお願いします。

###ご回答により修正結果

Take One さんにご回答を頂き修正しました。下記のスクリーンショットのごとく第2画面ができました。感謝です。

イメージ説明

イメージ説明

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

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

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

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

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

guest

回答1

0

ベストアンサー

セルに定義したUIImageViewをimageViewという変数に入れたいんでしょうから、

let imageView = image1.viewWithTag(1) as! UIImageView
じゃなくて
let imageView = cell.viewWithTag(1) as! UIImageView
だろうと思います。

あるいは、マークダウンが崩れてコードが見にくいですが、
カスタムセルにimage1というUIImageViewのプロパティを定義しているようですから、
let imageView = cell.image1
でもいいと思います。
ちゃんとカスタムセルを定義したんだったら、タグを使うよりこちらの方が私としてはお勧めです。

もちろん
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
でセルを取得した後に実行する必要があります。

投稿2016/09/23 15:43

編集2016/09/23 15:55
TakeOne

総合スコア6299

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

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

Tomzy

2016/09/24 01:36

Take One さんへ こんにちは。 ご助言ありがとうございました。 エラーがでたりして、格闘しています。 進捗次第ご報告します。
Tomzy

2016/09/24 04:28

下記の10 caseほどやって見ましたが、アイコン画像が表示されず,いずれも cannot assign value of type 'String' to 'UIImageView' または cannot assign value of type 'UIImage?' to type 'UIImageView?' のエラーがでてしまいます。 ご教示頂ければ幸いです。 なお、if文つきは当面はずし "Video_b.png"の表示のみとし 、下のコメントアウトはcaseごとにはずして試しました。 let image1 = UIImage.self let imgVideo = "Video_b.png" //省略 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell ///イメージ表示のテスト //case 01 //let imageView = cell.viewWithTag(1) as! UIImageView //imageView.image = imgVideo //case02 // let imageView = cell.viewWithTag(1) as! UIImageView // imageView.image = "Video_b.png" //case03 // let imageView = cell.viewWithTag(1) as! UIImageView // cell.image1 = imgVideo //case04 // let imageView = cell.viewWithTag(1) as! UIImageView // cell.image1 = "imageVideo" //case05 // let imageView = cell.viewWithTag(1) as! UIImageView // cell.image1 = UIImage(named: "Video_b.png") //case 06 //let imageView = cell.image1 //imageView.image = imgVideo //case07 // let imageView = cell.image1 // imageView.image = "Video_b.png" //case08 // let imageView = cell.image1 // cell.image1 = imgVideo //case09 // let imageView = cell.image1 // cell.image1 = "imageVideo" //case10 let imageView = cell.image1 cell.image1 = UIImage(named: "Video_b.png")
TakeOne

2016/09/24 14:34 編集

let imageView = cell.image1 imageView.image = UIImage(named: imgVideo) が正解だと思います。 なぜこれまでのコードが動いているのか、なぜ今回のコードがエラーになるのか、Swiftの言語仕様やiOS-SDKの仕様を理解し何をやっているのか論理的に考えてやらないと、エラーが出た時に目をつむって闇雲にあさっての方向に試し射ちしても無駄な鉄砲になるだけで当たらないと思いますよ。 それと、質問のコードのマークダウンが崩れているのでちゃんと直して見えるようにしてください。それを直してきちんとコードが見えていれば、imgVideoが文字列になっていて次の行も誤っているというのは最初の回答で指摘できたかもしれません。
Tomzy

2016/09/24 14:56

コメントありがとうございました。 全くおっしゃるとおりです。
Tomzy

2016/09/24 23:31

Take One さんへ こんにちは。 お陰様でつくりたい画面ができました。 上記編集画面にスクリーンショットを載せました。 ありがとうございました。
Tomzy

2016/09/27 05:09

Take One さんへ こんにちは コードの乱れについて、事務局に照会をしていました。私が誤ってコード記載欄の記号を一部消してしまったためと判明しました。編集欄でそれを修正しました。ご迷惑をおかけしました。ごめんなさい。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問