所以这是我的代码。我创建了一个带搜索栏的表格,我希望在搜索并点击后,它会显示另一个视图控制器的详细信息。我的想法是因为重新排列表行后indexpath是nill。但我不知道如何解决它。请帮助我
import UIKit
class STableViewController: UITableViewController, UISearchResultsUpdating {
var searchController: UISearchController!
var resultSearchController = UITableViewController()
var filterList = Array<String>()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController.tableView.delegate = self
self.resultSearchController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: resultSearchController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filterList = getList.filter({ (list: String) -> Bool in
if list.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString)
{
return true
}
else {
return false
}
})
self.resultSearchController.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == self.tableView {
return getList.count
}
else
{
return filterList.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell1 = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell1")
if tableView == self.tableView {
cell1.textLabel?.text = getList[indexPath.row]
}
else
{cell1.textLabel?.text = filterList[indexPath.row]
}
return cell1
}
override func viewDidAppear(animated: Bool) {
if self.tableView != nil {
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("SearchShow", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SearchShow")
{
if let destination = segue.destinationViewController as? DetailViewController
{
let indexPath = self.tableView.indexPathForSelectedRow
let titleString = getDesc[indexPath!.row]
destination.getDesc = titleString
self.tableView.deselectRowAtIndexPath(indexPath!, animated: true)
}
}
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
getDesc.removeAtIndex(indexPath.row)
getList.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(getList, forKey: "Name")
NSUserDefaults.standardUserDefaults().setObject(getDesc, forKey: "Desc")
self.tableView.reloadData()
}
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}