用于 TableView 的 UISearchController

UISearchController for TableView

这就是我的 Table 视图的样子。每个单元格串起 .txt 中的文本

搜索过滤器工作正常。但是当我 select 其中一个结果时,例如"How" 它打印 "Hello" 的内容。

我想这可能是因为 "Hello" 被编程为第一个字符串,因此它始终是单击第一个单元格的结果。我需要帮助来弄清楚搜索结果如何链接到它自己的内容。如果那有意义的话?

这是下面的代码。代码从 .txt 文件中读取并搜索文本文件。单击时的结果不匹配。

导入 UIKit

class title_ViewController: UITableViewController, UISearchResultsUpdating {
var SongTitle = [String]()
var SongLyrics = [LText]()
var filteredSongs = [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()

    if let path = NSBundle.mainBundle().pathForResource("DataBase", ofType:     "txt"){
        do {
            let stringFromFile = try String(contentsOfFile:path, encoding:     NSUTF8StringEncoding)
            var Title: [String] =     stringFromFile.componentsSeparatedByString("@")
            Title.removeAtIndex(0)
            //Seperates the Title from the Lyrics
            var t1 = Array(Title[0].componentsSeparatedByString("^"))
            t1.removeAtIndex(0)

            SongTitle = Title
            SongLyrics = [LText(LyricsText: t1)]

        }catch{
            print(error)
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if self.resultSearchController.active{
        return self.filteredSongs.count
    }else{
        return self.SongTitle.count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let Cell: AnyObject = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    Cell.textLabel?!.text = SongTitle[indexPath.row]


    if self.resultSearchController.active{
        Cell.textLabel?!.text = self.filteredSongs[indexPath.row]
    }else{
        Cell.textLabel?!.text = self.SongTitle[indexPath.row]
    }


    return Cell as! UITableViewCell

}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.filteredSongs.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c]%@", searchController.searchBar.text!)

    let array = (self.SongTitle as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredSongs = array as! [String]
    self.tableView.reloadData()
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
    let DestViewController = segue.destinationViewController as! ViewController

    DestViewController.LyricString = SongTitle[indexPath.row]
}
}

欢迎任何帮助,提前致谢。

问题出在你的prepareForSegue。您需要修改它以测试 searchController 是否处于活动状态。如果是这样,您应该使用 filteredSongs 来确定将哪首歌传递给目标 VC,而不是 SongTitle:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
    let DestViewController = segue.destinationViewController as! ViewController

    if self.resultSearchController.active {
        DestViewController.LyricString = self.filteredSongs[indexPath.row]
    }else{
        DestViewController.LyricString = self.SongTitle[indexPath.row]
    }
}

(顺便说一句,变量名应该以小写字母开头 - songTitle 而不是 SongTitle,等等)

您可以在 didSelectRowAtIndexPath 中通过从数组中搜索所选字符串的位置来手动执行 segue,然后您可以获得正确的位置 -

示例 -

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    let Cell: AnyObject = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let str: String=  Cell.textLabel.text
    // now find str in array and then get position
    int pos = getPositionOfStringInArray(str)        

    // now perform segue for pos
    //code...

}