ios - 搜索后访问索引路径行

标签 ios xcode swift uisearchcontroller

我用数据填充表格,所有工作都按预期进行 - 包括将每行的详细信息连接到另一个场景。我在其中放置了一个搜索栏(以编程方式 - 使用新的 searchController),它成功地重新加载了原始表以及任何找到的结果。

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

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

这是单元格创建:

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 中是否存在事件的搜索结果。

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 有一个很棒的教程:

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

关于ios - 搜索后访问索引路径行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30739258/

相关文章:

ios - 可能有一个 UINavigationController 用于不填充整个 super UIView 的 UITableView

ios - ReplayKit 初始警报选择的委托(delegate)方法是什么?

ios - 如何全局设置工具栏项上的Inset?

ios - 在子类中根据 UIButton 大小设置 cornerRadius 的最佳位置?

ios - 独立的 iMessage 应用程序在 iOS 12 中表现不同

ios - 如何遍历数组并根据字符串值删除项目

c++ - 如何将 boost 库添加到 Xcode 11? C++

swift - Swift 中的 long long 常量

ios - iOS 6 中的模态视图 Controller

ios - 我是否需要增量获取 CloudKit 更改和订阅?