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

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

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

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

Swift

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

Q&A

解決済

1回答

2207閲覧

TableViewCell 画像配置 エラー

taiki9696

総合スコア17

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/01/29 03:38

編集2017/01/29 03:45

TableViewCellsを使って画面推移ができるコードを記述しています。ページ推移は可能ですが、画像を加えるためのコードを記述するとエラーが出てしまいます。

画面推移置で参考にした動画
https://www.youtube.com/watch?v=BCSGh-YJgvs&t=174s
画像配置で参考にしたサイト
http://joyplot.com/documents/2016/11/11/swift-uitableview/

import

1import UIKit 2 3class TableViewController: UITableViewController { 4 5 var names = [String]() 6 var identities = [String]() 7 var imgArray = [String]() 8 9 override func viewDidLoad() { 10 11 names = ["セル1","セル2"] 12 identities = ["A","B"] 13 imgArray = ["img1","img2"] 14 } 15 16 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 return names.count 18 } 19 20 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 // tableCell の ID で UITableViewCell のインスタンスを生成 22 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 23 cell?.textLabel!.text = names[indexPath.row] 24 25 //画像を加えるためのコード 26 27 let img = UIImage(named:"\(imgArray[indexPath.row])") 28 29 let imageView = tableView.viewWithTag(1) as! UIImageView 30 imageView.image = img 31 32 33 return cell! 34 } 35 36 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 37 let vcName = identities[indexPath.row] 38 let ViewController = storyboard?.instantiateViewController(withIdentifier: vcName) 39 self.navigationController?.pushViewController(ViewController!, animated: true) 40 } 41} 42

エラー
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-01-29 12:25:58.293533 TableViewjerd[1036:275067] fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

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

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

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

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

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

guest

回答1

0

ベストアンサー

StoryboardのUITableViewCellIdentifierCellにして、セルに乗せたUIImageViewtag1にしていることを確認してください。

あと以下の部分が記述間違えだと思うので書きなおしてみてください。

swift

1let imageView = tableView.viewWithTag(1) as! UIImageView 2 34 5let imageView = cell?.viewWithTag(1) as! UIImageView

投稿2017/01/29 04:01

編集2017/01/29 04:18
_Kentarou

総合スコア8490

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

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

taiki9696

2017/01/29 05:16 編集

imageViewの書き換えでエラーが消えました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問