###前提・実現したいこと
タイトルにも書いたのですが
コードで書いたテーブルビューのセルをカスタムセル(TableViewCell.xib)で
表示したいです
###発生している問題・エラーメッセージ
fatal error: unexpectedly found nil while unwrapping an Optional value 2016-12-25 23:59:30.890628 teburutesuto[1430:1088920] fatal error: unexpectedly found nil while unwrapping an Optional value
###該当のソースコード
ViewController側 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! TableViewCell // // // if tableView == myTableView[0] { cell.Label1.text = "\(raberuiti[0][indexPath.row])" cell.Label2.text = "\(raberuni[0][indexPath.row])" } else if tableView == myTableView[1] { cell.Label1.text = "\(raberuiti[1][indexPath.row])" cell.Label2.text = "\(raberuni[1][indexPath.row])" } // return cell }
テーブルビューを配列に入れてスクロールビュー上に乗せています。
TableViewCell側 import UIKit class TableViewCell: UITableViewCell { // @IBOutlet weak var mySwitch: UISwitch! @IBOutlet weak var Label1: UILabel! @IBOutlet weak var Label2: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
###試したこと
if tableView == myTableView[0] { cell.Label1.text = "\(raberuiti[0][indexPath.row])" cell.Label2.text = "\(raberuni[0][indexPath.row])" } else if tableView == myTableView[1] { cell.Label1.text = "\(raberuiti[1][indexPath.row])" cell.Label2.text = "\(raberuni[1][indexPath.row])" }
この部分を消したら、エラーが出ないので問題は
ここだと思うのですがカスタムセルのパーツの使い方が間違っているのでしょうか?
###補足情報(言語/FW/ツール等のバージョンなど)
import UIKit class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource { // UIScrollView. var scrollView: UIScrollView! // TableViewのアイテム. let raberuiti: [[String]] = [["ご飯", "味噌汁", "焼き魚"],["天津飯", "チャーハン", "八宝菜", "酢豚", "肉まん"]] let raberuni: [[String]] = [["1980/2/27", "1983/8/25", "2016/10/10"],["2016/6/24", "2014/12/16", "2014/8/16", "2014/11/16", "2018/12/3"]] // TableView. var myTableView: [UITableView] = [] // ページ番号. let pageSize = 2 override func viewDidLoad() { super.viewDidLoad() // ScrollViewの設定. let width = self.view.frame.maxX scrollView = UIScrollView(frame: self.view.frame) scrollView.showsHorizontalScrollIndicator = false; scrollView.showsVerticalScrollIndicator = false scrollView.isPagingEnabled = true scrollView.delegate = self scrollView.contentSize = CGSize(width:CGFloat(pageSize) * width, height:0) self.view.addSubview(scrollView) // Status Barの高さを取得. let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height // Viewの高さと幅を取得. let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height for i in 0 ..< pageSize { let tableView: UITableView = UITableView(frame: CGRect(x: CGFloat(i) * width, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) tableView.estimatedRowHeight = 60 tableView.rowHeight = UITableViewAutomaticDimension tableView.tag = i tableView.dataSource = self tableView.delegate = self tableView.register(TableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.append(tableView) scrollView.addSubview(myTableView[i]) } print("myTableView[]: \(myTableView)") } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { // スクロール数が1ページ分になったら. if fmod(scrollView.contentOffset.x, scrollView.frame.maxX) == 0 { // ページの場所を切り替える. // pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.frame.maxX) } } /* Cellが選択された際に呼び出される. */ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView.tag == 0 { print("Num: \(indexPath.row)") } else if tableView.tag == 1 { print("Num: \(indexPath.row)") } } /* Cellの総数を返す. */ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView.tag == 0 { return raberuiti[0].count } else if tableView.tag == 1 { return raberuiti[1].count } else{ return 0 } } /* Cellに値を設定する. */ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! TableViewCell // // print("tableView.tag: \(tableView.tag)") print("indexPath.row: \(indexPath.row)") // cell.Label1.text = raberuiti[tableView.tag][indexPath.row] cell.Label2.text = raberuni[tableView.tag][indexPath.row] return cell } }
swift3.0 xcode8.3
どこでエラーが出ているのでしょうか?また、cellForRowAtの最初に print(tableView.tag, indexPath.row) を追加して、エラーが出る直前の出力を教えて下さい。
tableView.tag: 1 indexPath.row: 0 fatal error: unexpectedly found nil while unwrapping an Optional value 2016-12-26 11:25:10.870700 teburutesuto[1504:1153021] fatal error: unexpectedly found nil while unwrapping an Optional value
"Add Exception Breakpoint"を設定して、どこで止まっているか調べて下さい。(参考:http://engineer-blog.vermuda.jp/?p=90)
回答2件
あなたの回答
tips
プレビュー