我已经成功构建了一个搜索功能,它在detailviewcontroller中搜索并显示TableView标题和副标题。在detailviewcontroller中有两个文本标签,一个用于标题,一个用于字幕。数组索引超出范围
在单元格中显示标题和字幕效果很好,包括点击并在detailviewcontroller中看到它们。但在搜索功能的应用程序崩溃打字因时:
fatal error: Array index out of range (lldb)
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
这里是我的所有代码:
let quotes = [
"Saying1",
"Saying2",]//End
let persons = [
"Name1",
"Name2",
]//End
var filteredQuotes = [String]()
var filteredPersons = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1 }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if self.resultSearchController.active
{
return self.filteredQuotes.count
}
else
{
return self.quotes.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell?
if self.resultSearchController.active
{
cell!.textLabel?.text = self.filteredQuotes[indexPath.row]
cell!.detailTextLabel?.text = self.filteredPersons[indexPath.row] //THE ERROR OCCURS HERE
}
else
{
cell!.textLabel?.text = self.quotes[indexPath.row]
cell!.detailTextLabel?.text = self.persons[indexPath.row]
}
return cell!
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let destination = segue.destinationViewController as? SearchDetailViewController {
let path = tableView.indexPathForSelectedRow
let cell = tableView.cellForRowAtIndexPath(path!)
destination.viaSegue = (cell?.textLabel?.text!)!
destination.viaSeguePerson =(cell?.detailTextLabel?.text!)!
}
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
_ = tableView.indexPathForSelectedRow!
if let _ = tableView.cellForRowAtIndexPath(indexPath) {
self.performSegueWithIdentifier("SendDataSegue", sender: self)
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredQuotes.removeAll(keepCapacity: false)
self.filteredPersons.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS [c] %@", searchController.searchBar.text!)
let Quotesarray = (self.quotes as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredQuotes = Quotesarray as! [String]
let Personsarray = (self.persons as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredPersons = Personsarray as! [String]
self.tableView.reloadData()
}
发布完整的崩溃日志并正确标记您的问题(删除'xcode'并添加'swift')。 – trojanfoe
“filteredPersons”包含您认为包含的数据吗?你记录了内容吗?你为什么要将数据存储在2个独立的数组中?它在我看来就像一个属于一个人的引用是正确的?为什么不制作一个名为'Quote'的对象,该对象包含一个用于引用的字符串和一个用于名称的字符串?那么你只需要一个名为'quotes'的数组或其他东西。 – Fogmeister
什么保证你'Quotesarray'和'Personsarray'包含相同的数量(以及相应的数据)?例如:引用@“我认为,因此我是......”,以及“RenéDescartes”的人,并且你搜索“Descartes”,Quotesarray和Personsarray应该是什么? – Larme