前提・実現したいこと
初心者ながらswiftで簡易的なSNSを開発したいと思っています。
はじめからSNSを開発できるとは思えないので1つ1つ機能を作り、それを積み重ねていき、最終的にSNSを作れればと思っています。
その過程でInstagramやツイッターなどにある検索バーを作っています。
検索バーの機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
###ストーリーボードのUI部品の配置
ソースコード
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { 4 5 @IBOutlet weak var searchBar: UISearchBar! 6 @IBOutlet weak var tableView: UITableView! 7 8 9 10 //検索データ 11 let kensaku:[String] = [] 12 13 var searchResults:[String] = [] 14 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 20 //tableViewのカスタマイズ 21 22 view = UIView() 23 let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height 24 tableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: self.view.frame.width, height: self.view.frame.height)) 25 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 26 tableView.dataSource = self 27 tableView.delegate = self 28 self.view.addSubview(tableView) 29 //このテーブルビューのコード上のどこかで必ずエラーが発生 30 31 32 //searchBarのカスタマイズ 33 searchBar = UISearchBar() 34 searchBar.delegate = self 35 searchBar.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:42) 36 searchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 89) 37 searchBar.searchBarStyle = UISearchBarStyle.default 38 searchBar.showsSearchResultsButton = false 39 searchBar.placeholder = "検索" 40 searchBar.setValue("キャンセル", forKey: "_cancelButtonText") 41 searchBar.tintColor = UIColor.red 42 43 tableView.tableHeaderView = searchBar 44 } 45 46 override func didReceiveMemoryWarning() { 47 super.didReceiveMemoryWarning() 48 } 49 50 //データの個数を返すメソッド 51 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 52 if searchBar.text != "" { 53 return searchResults.count 54 } else { 55 return kensaku.count 56 } 57 } 58 //tableViewCellの作成&検索結果表示 59 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 60 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) 61 if searchBar.text != "" { 62 cell.textLabel!.text = "(searchResults[indexPath.row])" 63 } else { 64 cell.textLabel!.text = "(kensaku[indexPath.row])" 65 } 66 67 return cell 68 } 69 70 // 検索ボタンが押された時に呼ばれる 71 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 72 self.view.endEditing(true) 73 searchBar.showsCancelButton = true 74 self.searchResults = kensaku.filter{ 75 // 大文字と小文字を区別せずに検索 76 $0.lowercased().contains(searchBar.text!.lowercased()) 77 } 78 self.tableView.reloadData() 79 } 80 81 // キャンセルボタンが押された時に呼ばれる 82 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 83 searchBar.showsCancelButton = false 84 self.view.endEditing(true) 85 searchBar.text = "" 86 self.tableView.reloadData() 87 } 88 89 // テキストフィールド入力開始前に呼ばれる 90 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 91 searchBar.showsCancelButton = true 92 return true 93 } 94}
エラーをなくすために試したこと
1、エラー部分のコードを消す
・エラー部分のコードを消しても違う部分にエラーが出てしまい、その繰り返しになってしまいました。
2、以前ストーリーボード上にViewを生成しておらず、self.viewの部分がアンラップに失敗しているというご指摘をいただき、ストーリーボード上にViewを追加させていただきました。
・エラー箇所は変わりましたが、以前上記のエラーが解消されることはありませんでした。
補足情報
元々はコードを主体として開発を行っていたのですが視覚的に見やすくしたいと思い、
コードのみでのシミュレーター実装ができる状態に仕上げてからストーリーボードを使い、
UI部品を接続し、接続時のコードと被っているコードを消していくという作業をしていった際にどこかでNilが発生してしまったのだと思います。
あなたの回答
tips
プレビュー