搜索后访问索引路径行 [英] Accessing index path row after search

查看:24
本文介绍了搜索后访问索引路径行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用数据填充了一个表格,所有的工作都按预期进行——包括将每一行的细节转移到另一个场景.我在那里扔了一个搜索栏(以编程方式 - 使用新的 searchController),它成功地重新加载了包含任何找到的结果的原始表.

I populate a table with data and all works as intended - including segueing each row's details to another scene. I throw a search bar in there (programmatically - using the new searchController) and it successfully reloads the original table with any found results.

然而,在搜索后触摸选定的行时,传递的是原始表行的顺序,该行恰好与现在触摸的行处于同一位置!(即,如果我选择当前搜索的第二行,下一个场景将继续原表第二行的详细信息!)

HOWEVER, when touching a selected row after a search, the segue passed along is that of the original table row that happens to be in the same position of the one touched now! (i.e. if I choose the current second row of a search, the next scene will segue the details of the second row of the original table!)

那么如何在搜索后为 prepareForSegue 函数提供正确的行?

So how do I provide the prepareForSegue function my correct row after a search?

这是单元格创建:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    if (self.resultSearchController.active) {
        cell.textLabel?.text = filteredTableData[indexPath.row]

        return cell
         }
    else {

    cell.textLabel?.text = TableTitle[indexPath.row]

return cell
}
}

这是搜索功能:

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
    let array = (the_tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}

这是转场:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using [segue destinationViewController].

    var thirdScene = segue.destinationViewController as! customer_details_View_Controller
    if let indexPath = self.tableView.indexPathForSelectedRow() {

        thirdScene.basics = the_basics[indexPath.row]
        thirdScene.p_method = the_p_method[indexPath.row]
        thirdScene.notes = the_notes[indexPath.row]

    }

    // Pass the selected object to the new view controller.
}

推荐答案

您还必须检查 prepareFOrSegueDidSelectRowAtIndexPath 中的搜索结果是否处于活动状态.

You have to check for search result active in prepareFOrSegue or DidSelectRowAtIndexPath as well.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].

var thirdScene = segue.destinationViewController as! customer_details_View_Controller

if self.resultSearchController.active && searchController.searchBar.text != "" {
// Select data from Filtered Array
} else {
 // Select data from regular array
}
}

Rey Wenderlich 有一个很好的教程:

Rey Wenderlich has an excellent tutorial for this:

https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

这篇关于搜索后访问索引路径行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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