LoginSignup
0
1

More than 5 years have passed since last update.

質問です。

Posted at

⚠️のところで、Fatal error: Unexpectedly found nil while unwrapping an Optional value
が出てしまいました。原因がわからず困っています。助けてください。
(なお、コードを書く練習のために、@Simmonさんの記事を拝借させていただきました。)

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

let PPAP:[String] = ["PPAP", "I have a pen", "I have a apple", "Uh! Apple-Pen!", "I have a pen", "I have pineapple", "Uh! Pineapple-Pen!", "Apple-Pen, Pineapple-Pen", "Uh! Pen-Pineapple-Apple-Pen", "Pen-Pineapple-Apple-Pen"]
var searchResults:[String] = []

override func viewDidLoad() {
super.viewDidLoad()

let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
tableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: self.view.frame.width, height: self.view.frame.height))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "PPAP") ⚠️
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)

searchBar = UISearchBar()
searchBar.delegate = self
searchBar.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:42)
searchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 89)
searchBar.searchBarStyle = UISearchBar.Style.default
searchBar.showsSearchResultsButton = false
searchBar.placeholder = "検索"
searchBar.setValue("キャンセル", forKey: "_cancelButtonText")
searchBar.tintColor = UIColor.red

tableView.tableHeaderView = searchBar

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchBar.text != "" {
return searchResults.count
} else {
return PPAP.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
if searchBar.text != "" {
cell.textLabel!.text = "(searchResults[indexPath.row])"
} else {
cell.textLabel!.text = "(PPAP[indexPath.row])"
}

return cell

}

// 検索ボタンが押された時に呼ばれる
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.view.endEditing(true)
searchBar.showsCancelButton = true
self.searchResults = PPAP.filter{
// 大文字と小文字を区別せずに検索
$0.lowercased().contains(searchBar.text!.lowercased())
}
self.tableView.reloadData()
}

// キャンセルボタンが押された時に呼ばれる
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
self.view.endEditing(true)
searchBar.text = ""
self.tableView.reloadData()
}

// テキストフィールド入力開始前に呼ばれる
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsCancelButton = true
return true
}
}

0
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1