###前提・実現したいこと
http://qiita.com/ytakzk/items/d7bb8182d43cdfc9b580
上記を参考にカスタムセルの高さ自動調整のサンプルを動かしたいです。
###発生している問題・エラーメッセージ
実行すると
let cell: MyTableViewCell = tableView?.dequeueReusableCellWithIdentifier("Cell") as MyTableViewCell
の箇所で以下のエラーが発生してしまいます。
fatal error: unexpectedly found nil while unwrapping an Optional value
対象がnil1のため発生するエラーのようですが、原因、対処方法がわかりません。
よろしくお願いいたします。
###該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDataSource { 4 5 struct Article { 6 var title:String 7 var userName:String 8 } 9 10 var articles: Array<Article>? 11 12 @IBOutlet var tableView: UITableView! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 18 articles = [ 19 Article(title: "私わたくしはその人を常に先生と呼んでいた。だからここでもただ先生と書くだけで本名は打ち明けない。", userName: "夏目漱石"), 20 Article(title: "この書の世に出づるにいたりたるは、函館にある秦慶治氏、及び信濃にある神津猛氏のたまものなり。労作終るの日にあたりて、このものがたりを二人の恩人のまへにさゝぐ。", userName: "島崎藤村"), 21 Article(title: "散文に二種あると考へてゐるが、一を小説、他を作文とかりに言つておく。", userName: "坂口安吾"), 22 Article(title: "機敏な晩熟児といふべき此の男が、現に存するのだから僕は機敏な晩熟児が如何にして存るママかその様を語らうと思ふ。", userName: "中原中也") 23 ] 24 25 self.tableView.estimatedRowHeight = 90 26 self.tableView.rowHeight = UITableViewAutomaticDimension 27 tableView.dataSource = self 28 29 } 30 31 override func didReceiveMemoryWarning() { 32 super.didReceiveMemoryWarning() 33 // Dispose of any resources that can be recreated. 34 } 35 36 37 func tableView(_ tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 38 return articles!.count 39 } 40 41 func tableView(_ tableView: UITableView?, cellForRowAt indexPath:IndexPath!) -> UITableViewCell! { 42 let cell: MyTableViewCell = tableView?.dequeueReusableCell(withIdentifier: "Cell") as! MyTableViewCell 43 cell.title.text = articles?[indexPath.row].title 44 cell.userName.text = articles?[indexPath.row].userName 45 cell.layoutIfNeeded() 46 return cell; 47 } 48 49}
swift
1import UIKit 2 3class MyTableViewCell: UITableViewCell { 4 5 @IBOutlet var title: UILabel! 6 @IBOutlet var userName: UILabel! 7 8 override func awakeFromNib() { 9 super.awakeFromNib() 10 // Initialization code 11 } 12 13 override func setSelected(_ selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 16 // Configure the view for the selected state 17 } 18 19}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。