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

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

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

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

Swift

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

Q&A

解決済

1回答

1751閲覧

TableViewにて、viewWithTagとregisterの併用が出来ない

aucks

総合スコア7

iOS

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

Swift

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

0グッド

0クリップ

投稿2017/02/20 09:19

編集2017/02/20 09:48

###前提・実現したいこと
TableView上に、viewWithTagで設定したUILabel等を表示させようとしています。
しかし、viewWithtag部分でエラーが発生してしまい、先に進めません。

###発生している問題・エラーメッセージ

エラー箇所は、 circleLabelの宣言部分で発生しています。
エラーメッセージは以下のように出ています。

thread 1 exc_bad_instruction(code=exc_1386_invop subcode=0x0)

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

// // HomeController.swift // import Foundation import UIKit import MMDrawerController class HomeController: UIViewController, UITableViewDelegate, UITableViewDataSource{ @IBOutlet weak var deviceTableView: UITableView! let FirstLabelTag = 1 let SecondLabelTag = 2 let ThreadLabelTag = 3 var row:Int = 0 override func viewDidLoad() { navigationController!.setNavigationBarHidden(true, animated: false) super.viewDidLoad() deviceTableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceCell") deviceTableView.delegate = self deviceTableView.dataSource = self let v:UIView = UIView(frame: CGRect.zero) v.backgroundColor = UIColor.clear deviceTableView.tableFooterView = v deviceTableView.tableHeaderView = v deviceTableView.separatorColor = .clear } override func viewDidAppear(_ animated: Bool) { app.ud.set("home", forKey:"History") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { row = (indexPath as NSIndexPath).row print(indexPath.row) app.ud.set(row, forKey: "deviceID") moveToThermoData() tableView.deselectRow(at: indexPath, animated: false) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let devices = app.realm.objects(Device.self) return (devices.count) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceCell", for: indexPath) // エラー発生箇所 let circleLabel = tableView.viewWithTag(FirstLabelTag) as! CustomLabel circleLabel.layer.cornerRadius = CustomLabel.cornerRadiusForRect(circleLabel.bounds) let statusLabel = tableView.viewWithTag(SecondLabelTag) as! UILabel // Debug statusLabel.text = "FINE" statusLabel.font = UIFont(name: "STHeitiTC-Light", size: circleLabel.frame.width*0.17) let deviceNameLabel = tableView.viewWithTag(ThreadLabelTag) as! UILabel deviceNameLabel.text = "\((app.realm.objects(Device.self)[(indexPath as NSIndexPath).row].deviceName) as String)" deviceNameLabel.font = UIFont(name: "STHeitiTC-Light", size: 15) return cell } func moveToThermoData() { // Loading中のフラグ app.isGraphFinish = false let thermoDataController = storyboard!.instantiateViewController(withIdentifier: "ThermoData") as! ThermoDataController let thermoNavController = UINavigationController(rootViewController: thermoDataController) let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.centerContainer!.centerViewController = thermoNavController appDelegate.centerContainer!.toggle(MMDrawerSide.left, animated: true, completion: nil) } }

###試したこと

  • deviceTableView.registerの部分を消すと正常に動作するのですが、セルの選択が一切出来なくなってしまいます。
  • circleLabelの宣言部分を削除すると、statusLabel,deviceNameLabelの宣言部分で同様のエラー発生
  • StroyBoardにて、各LabelのTagをcircleLabel=1,statusLabel=2,deviceNameLabel=3のように設定

参考サイト

[iPhone] UITableView をストーリボードで作る
[Swift] storyboardを使わず、テーブルを表示させる短いサンプル(UITableView)
[Swift3] UITableViewの基本的な使い方とリスト作成サンプル

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

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

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

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

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

t_obara

2017/02/20 09:28

タグが全て同じ1になっているからでは?別の数値にしてみてはいかがでしょうか?
aucks

2017/02/20 09:33

ありがとうございます。変数部分を修正しましたが、同じエラーが出てしまいます。
t_obara

2017/02/20 09:42

参考にした資料やwebがあるのであれば、それも提示されると良いかもしれません。色々と問題がありそうですし。タグはStoryboardで設定しているのですか?viewの親子関係は想定通りになっていますか?
aucks

2017/02/20 09:51

ありがとうございます。参考サイトの情報を更新しました。タグはStoryboardで設定しました。viewの親子関係は想定通りになっているはずです。
t_obara

2017/02/20 09:57

参考にしたサイトの一番上が近いのですが、そちらにないコード(registerとか)は不要。タップされた時の処理は当該サンプルに記載がないので、タップされた時の処理を追加する必要がありますが、それは貴殿が追加した処理ではありません。中途半端にツギハギコードにするのではなく、一つずつ内容を理解するようにされてはいかがでしょうか。
aucks

2017/02/20 10:01

そうですね... 中途半端にツギハギで作ってしまっていたので、1つずつ内容を理解して書き込んでいくようにしていきます。ありがとうございます。
guest

回答1

0

ベストアンサー

Storyboard上でカスタムセルを作っているのであればregister()は必要ありません。

セルの選択が一切出来ないというのは、tableView(_:didSelectRowAt:)が呼ばれないということでしょうか?

【追記】

Storyboardのセルの設定で、Attributes inspectorのViewの項目にあるUser Interaction Enabledにチェックは入っているでしょうか?

投稿2017/02/20 10:50

編集2017/02/21 04:55
fuzzball

総合スコア16731

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

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

aucks

2017/02/21 04:18

ありがとうございます。 そうですね、tableView(_:didSelectRowAt:)部分が呼び出されていないらしいですね。
fuzzball

2017/02/21 04:43

「らしい」というのはどういうことでしょうか?
fuzzball

2017/02/21 04:55

回答に追記しました。
aucks

2017/02/21 05:00

すみません、らしいではなく、「呼び出されていない」です。 追記してもらった内容を試したところ、セルを選択できるようになりました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問