我试图获取一个搜索栏来搜索表视图的内容,但我无法弄清楚为什么会出现此错误。错误是:“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使标识符为Cell的单元出队-必须为该标识符注册一个笔尖或一个类,或者在情节提要中连接原型单元”。问题是我确实有一个原型单元,并且表格视图可以使用该单元。这仅在我单击搜索栏时发生。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
print("at the beginning of search bar")
if searchBar.text == nil || searchBar.text == ""
{
searchIsActive = false
view.endEditing(true)
tableView.reloadData()
}
else
{
searchResuls = names.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchIsActive = true
tableView.reloadData()
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
searchIsActive = false
searchBar.text = nil
tableView.reloadData()
searchBar.resignFirstResponder()
}
var names = [String]()
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
var searchIsActive = false
var searchResuls = [String]()
这是在我的表格视图功能中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
if searchIsActive
{
cell.firstNameLabel.text = searchResuls[indexPath.row]
// cell.firstNameLabel.text = searchResuls[indexPath.row].firstName
// names.append(studentInfo[indexPath.row].firstName!)
// cell.lastNameLabel.text = searchResuls[indexPath.row].lastName
// cell.CourseLabel.text = searchResuls[indexPath.row].course
// cell.TermYearLabel.text = searchResuls[indexPath.row].term! + ", " + String(studentInfo[indexPath.row].year!)
//
// //grabbing the image
// let url = URL(string: searchResuls[indexPath.row].imageURL!)
// let data = try? Data(contentsOf: url!)
// cell.ImageView.image = UIImage(data: data!)
}
else
{
cell.firstNameLabel.text = studentInfo[indexPath.row].firstName
names.append(studentInfo[indexPath.row].firstName!)
cell.lastNameLabel.text = studentInfo[indexPath.row].lastName
cell.CourseLabel.text = studentInfo[indexPath.row].course
cell.TermYearLabel.text = studentInfo[indexPath.row].term! + ", " + String(studentInfo[indexPath.row].year!)
//grabbing the image
let url = URL(string: studentInfo[indexPath.row].imageURL!)
let data = try? Data(contentsOf: url!)
cell.ImageView.image = UIImage(data: data!)
}
print("these are the names: ", names)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if searchIsActive
{
return searchResuls.count
}
else
{
return studentInfo.count
}
}