###前提・実現したいこと
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の基本的な使い方とリスト作成サンプル
タグが全て同じ1になっているからでは?別の数値にしてみてはいかがでしょうか?
ありがとうございます。変数部分を修正しましたが、同じエラーが出てしまいます。
参考にした資料やwebがあるのであれば、それも提示されると良いかもしれません。色々と問題がありそうですし。タグはStoryboardで設定しているのですか?viewの親子関係は想定通りになっていますか?
ありがとうございます。参考サイトの情報を更新しました。タグはStoryboardで設定しました。viewの親子関係は想定通りになっているはずです。
参考にしたサイトの一番上が近いのですが、そちらにないコード(registerとか)は不要。タップされた時の処理は当該サンプルに記載がないので、タップされた時の処理を追加する必要がありますが、それは貴殿が追加した処理ではありません。中途半端にツギハギコードにするのではなく、一つずつ内容を理解するようにされてはいかがでしょうか。
そうですね... 中途半端にツギハギで作ってしまっていたので、1つずつ内容を理解して書き込んでいくようにしていきます。ありがとうございます。
回答1件
あなたの回答
tips
プレビュー