用于 TableView 的 UISearchController [英] UISearchController for TableView

查看:17
本文介绍了用于 TableView 的 UISearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我的表格视图的样子.每个单元格将 .txt 中的文本字符串化

This is what my Table View looks like. Each cell Strings a text from a .txt

搜索过滤器工作正常.但是当我选择一个结果时,例如如何"它打印你好"的内容.

Search Filter works fine. But When I select one of the results e.g. "How" it prints the the Content of "Hello" instead.

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

I'm thinking this is probably because "Hello" is programmed as the first String so it will always be the result of the first cell when clicked. I am needing help with figuring out how the search results are linked to it's own content. if that makes any sense?

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

Here's the code below. The code reads from .txt file swell as search the text file. the results when clicked on do not match.

导入 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]
}
}

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

any help is welcomed, thanks in advance.

推荐答案

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

The problem lies in your prepareForSegue. You need to amend it to test whether the searchController is active. If so, you should use filteredSongs to determine which song to pass to the destination VC, rather than 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 等)

(As an aside, it is convention that variable names should start with a lowercase letter - songTitle not SongTitle, etc.)

这篇关于用于 TableView 的 UISearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆