我已经成功构建了一个搜索功能,可以在detailviewcontroller中搜索并显示TableView标题和副标题。在detailviewcontroller中有两个文本标签,一个用于标题,另一个用于字幕。
在单元格中显示标题和副标题可以正常工作,包括在detailviewcontroller中点击和查看它们。但是当在搜索功能中输入时,应用程序崩溃的原因是:
致命错误:数组索引超出范围 (lldb)
EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 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()
}
答案 0 :(得分:2)
正如@Larme和@Fogmeister所建议的那样,你可以通过一个结构化数组来改进这个 - 这里有一个如何做这个部分的例子。
定义一个结构来保存你的数据
struct Quotes
{
var person : String
var quote : String
}
和它的初始化
let quotes = [Quotes(person: "name 1", quote: "saying 1"),
Quotes(person: "name 2", quote: "saying 2") ]
或者,当然,你可以初始化一个空数组,然后在从某个地方检索它时附加数据 - 数据库或用户输入
var quotes : [Quotes] = []
let quote1 = Quotes(person: "name 3", quote: "saying 3")
quotes.append(quote1)
或
quotes.append(Quotes(person: "name 4", quote: "saying 4"))